温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中的服务怎么实现

发布时间:2021-12-18 17:01:54 来源:亿速云 阅读:154 作者:iii 栏目:移动开发

本篇内容主要讲解“Android中的服务怎么实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Android中的服务怎么实现”吧!

说明:这个例子实现了Android中常见的许多服务,下面是实现的截图

Android中的服务怎么实现

接下来,以源代码的方式分析这个例子

1.MainActivity--主界面

这个类主要是实现用户所看到的这个Activity,其中包含了一系列的按钮,用户点击按钮执行相应的动作,所以在这个类中主要是对按钮的定义和对按钮绑定相应的监听器,下面是实现的代码:

package lovefang.stadyService;      import android.app.Activity;   import android.os.Bundle;   import android.widget.Button;   import android.view.View;   import android.content.Intent;   import android.util.Log;   /**这是使用后台服务的学习例子*/   public class MainStadyServics extends Activity {           /**参数设置*/       Button startServiceButton;// 启动服务按钮       Button shutDownServiceButton;// 关闭服务按钮       Button startBindServiceButton;// 启动绑定服务按钮       Button sendBroadcast;// 使用广播       Button notificationButton;// 使用通知功能       Button alarmButton;// 使用闹钟       Button handlerButton;// 使用handler       Button asyncButton;// 使用异步加载       Button phoneStateButton;// 查看手机状态       Button callphoneButton;// 拨打电话       Button vibratorButton;// 使用震动        CountService countService;              @Override       public void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           Log.v("MainStadyServics", "setContentView");           setContentView(R.layout.main);           getWidget();           regiestListener();       }           /**获得组件*/       public void getWidget(){           startServiceButton = (Button)findViewById(R.id.startServerButton);           startBindServiceButton = (Button)findViewById(R.id.startBindServerButton);           shutDownServiceButton = (Button)findViewById(R.id.sutdownServerButton);           sendBroadcast = (Button)findViewById(R.id.sendBroadcast);           notificationButton = (Button)findViewById(R.id.notification);           alarmButton = (Button)findViewById(R.id.alarm);           handlerButton = (Button)findViewById(R.id.handler);           asyncButton = (Button)findViewById(R.id.async);           phoneStateButton = (Button) findViewById(R.id.phonestate);           callphoneButton = (Button) findViewById(R.id.callphone);           vibratorButton = (Button) findViewById(R.id.vibrator);       }           /**为按钮添加监听*/       public void regiestListener(){           startServiceButton.setOnClickListener(startService);           shutDownServiceButton.setOnClickListener(shutdownService);           startBindServiceButton.setOnClickListener(startBinderService);           sendBroadcast.setOnClickListener(broadcastReceiver);           notificationButton.setOnClickListener(notification);           alarmButton.setOnClickListener(startAlarm);           handlerButton.setOnClickListener(handler);           asyncButton.setOnClickListener(async);           phoneStateButton.setOnClickListener(phonestate);           callphoneButton.setOnClickListener(callphoneEvent);           vibratorButton.setOnClickListener(vibrator);       }           /**启动服务的事件监听*/       public Button.OnClickListener startService = new Button.OnClickListener(){           public void onClick(View view){                   /**单击按钮时启动服务*/               Intent intent = new Intent(MainStadyServics.this,CountService.class);               startService(intent);               Log.v("MainStadyServics", "start Service");           }       };           /**关闭服务*/       public Button.OnClickListener shutdownService = new Button.OnClickListener(){           public void onClick(View view){                   /**单击按钮时启动服务*/               Intent intent = new Intent(MainStadyServics.this,CountService.class);                   /**退出Activity是,停止服务*/               stopService(intent);               Log.v("MainStadyServics", "shutDown serveice");           }       };           /**打开绑定服务的Activity*/       public Button.OnClickListener startBinderService = new Button.OnClickListener(){           public void onClick(View view){                   /**单击按钮时启动服务*/               Intent intent = new Intent(MainStadyServics.this,UseBrider.class);               startActivity(intent);               Log.v("MainStadyServics", "start Binder Service");           }       };           /**打开广播学习的按钮*/       public Button.OnClickListener broadcastReceiver = new Button.OnClickListener(){           public void onClick(View view){               Intent intent = new Intent(MainStadyServics.this,UseBroadcast.class);               startActivity(intent);               Log.v("MainStadyServics","start broadcast");           }       };           /**打开通知*/       public Button.OnClickListener notification = new Button.OnClickListener(){           public void onClick(View view){               Intent intent = new Intent(MainStadyServics.this, UseNotification.class);               startActivity(intent);               Log.v("MainStadyService ","start Notification");                          }       };           /**使用闹钟*/       public Button.OnClickListener startAlarm = new Button.OnClickListener(){           public void onClick(View view){               Intent intent = new Intent(MainStadyServics.this, UseAlarmManager.class);               startActivity(intent);               Log.v("MainStadyService ","start alarm");                          }       };       public Button.OnClickListener handler= new Button.OnClickListener(){           public void onClick(View view){               Intent intent = new Intent(MainStadyServics.this, UseHandleMessage.class);               startActivity(intent);               Log.v("MainStadyService ","start handle");           }       };       public Button.OnClickListener async= new Button.OnClickListener(){           public void onClick(View view){               Intent intent = new Intent(MainStadyServics.this, UseAsyncTask.class);               startActivity(intent);               Log.v("MainStadyService ","start handle");           }       };       public Button.OnClickListener phonestate= new Button.OnClickListener(){           public void onClick(View view){               Intent intent = new Intent(MainStadyServics.this, UsePhoneState.class);               startActivity(intent);               Log.v("MainStadyService ","start phonestate");           }       };       public Button.OnClickListener callphoneEvent= new Button.OnClickListener(){           public void onClick(View view){               Intent intent = new Intent(MainStadyServics.this, UseActionCall.class);               startActivity(intent);               Log.v("MainStadyService ","start callphone");           }       };       public Button.OnClickListener vibrator= new Button.OnClickListener(){           public void onClick(View view){               Intent intent = new Intent(MainStadyServics.this, UseVibrator.class);               startActivity(intent);               Log.v("MainStadyService ","start callphone");           }       };           /***/       protected void onDestroy(){           super.onDestroy();           Intent intent = new Intent(MainStadyServics.this,CountService.class);               /**退出Activity是,停止服务*/           stopService(intent);       }                     }

2.启动服务按钮

这个类实现的是***个按钮的功能,在这个类中新开了一个线程,并每隔一秒打印出一行日志

代码如下:

package lovefang.stadyService;   /**引入包*/       import android.app.Service;// 服务的类       import android.os.IBinder;       import android.os.Binder;       import android.content.Intent;       import android.util.Log;   /**计数的服务*/       public class CountService extends Service{               /**创建参数*/           boolean threadDisable ;           int count;                      public IBinder onBind(Intent intent){               return null;           }           public void onCreate(){               super.onCreate();                   /**创建一个线程,每秒计数器加一,并在控制台进行Log输出*/               new Thread(new Runnable(){                   public void run(){                       while(!threadDisable){                           try{                               Thread.sleep(1000);                           }catch(InterruptedException e){                                                          }                           count++;                           Log.v("CountService","Count is"+count);                       }                   }               }).start();           }           public void onDestroy(){               super.onDestroy();                   /**服务停止时,终止计数进程*/               this.threadDisable = true;           }           public int getConunt(){               return count;           }           class ServiceBinder extends Binder{               public CountService getService(){                   return CountService.this;               }           }       }

3.绑定服务

服务有两种实现的方法:

(1)startService,启动服务,这时需要程序员管理服务的生命周期

(2)bindService,绑定服务,此时Service与Activity绑定在一起

下面是实现的代码:

package lovefang.stadyService;   /**引入包*/       import android.app.Activity;       import android.content.ComponentName;       import android.content.Context;       import android.content.Intent;       import android.content.ServiceConnection;       import android.os.Bundle;       import android.os.IBinder;       import android.util.Log;      /**通过bindService和unBindSerivce的方式启动和结束服务*/       public class UseBrider extends Activity {               /**参数设置*/           CountService countService;                  @Override           public void onCreate(Bundle savedInstanceState) {               super.onCreate(savedInstanceState);               setContentView(new UseBriderFace(this));               Intent intent = new Intent(UseBrider.this,CountService.class);                   /**进入Activity开始服务*/               bindService(intent, conn, Context.BIND_AUTO_CREATE);                          }           private ServiceConnection conn = new ServiceConnection(){                   /**获取服务对象时的操作*/                public void onServiceConnected(ComponentName name, IBinder service) {                   // TODO Auto-generated method stub                   countService = ((CountService.ServiceBinder)service).getService();                                  }                   /**无法获取到服务对象时的操作*/               public void onServiceDisconnected(ComponentName name) {                   // TODO Auto-generated method stub                   countService =null;               }                                         };           protected void onDestroy(){               super.onDestroy();               this.unbindService(conn);               Log.v("MainStadyServics", "out");           }       }

接下来为您介绍发送广播Notification通知、Alarm 闹钟等服务的具体内容


4.发送广播

使用sendBroadcast,向一个Action发送广播,并由相应的广播接收器接收并执行相应的动作

实现的代码如下:

       (1)打开广播服务

package lovefang.stadyService;   /**引入包*/       import android.view.View;       import android.os.Bundle;       import android.app.Activity;       import android.content.Intent;       import android.widget.Button;   /**使用Broadcast,这是一个发送广播的类*/       public class UseBroadcast extends Activity{               /**创建参数*/           private Button sendBroadcast;               /**创建Activity*/           public void onCreate(Bundle savedInstanceState){               super.onCreate(savedInstanceState);               setContentView(R.layout.broadcast);// 使用布局文件               getView();               sendBroadcast.setOnClickListener(sendBroadcastClick);// 添加事件监听           }           public void getView(){               sendBroadcast = (Button)findViewById(R.id.sendBroadcast);           }               /**创建事件监听*/           public Button.OnClickListener sendBroadcastClick = new Button.OnClickListener(){               public void onClick(View view){                   Intent intent = new Intent();// 创建意图                   intent.putExtra("CONTENT",  "This is a Braodcast demo");// 设置广播的内容                   intent.setAction("lovefang.stadyService");// 设置广播的Action                   sendBroadcast(intent);               }           };                  }

        (2 )处理广播消息

package lovefang.stadyService;   /***/       import android.content.BroadcastReceiver;       import android.content.Context;       import android.content.Intent;       import android.util.Log;   /**这是一个接收广播的类*/       public class UseBroadcastReceiver extends BroadcastReceiver{           public void onReceive(Context context, Intent intent){               Log.v("UseBroadcastReceiver", "I get a message");           }       }

5.Notification通知

这个称之为通知,显示在手机的通知栏,用户可以清除,可以点击

实现的代码如下:

  1. package lovefang.stadyService;   

  2.    

  3.     import android.content.Intent;   

  4.     import android.os.Bundle;   

  5.     import android.app.Activity;   

  6.     import android.app.Notification;   

  7.     import android.app.NotificationManager;   

  8.     import android.app.PendingIntent;   

  9.     import android.net.Uri;   

  10.     import android.media.RingtoneManager;   

  11.     import android.widget.Button;   

  12.     import android.view.View;   

  13.    

  14. /**使用notification*/   

  15.     public class UseNotification extends Activity {   

  16.             /**创建组件*/   

  17.         private Button textButton;   

  18.         private Button soundButton;// 声音通知   

  19.         private Button vibrateButton;// 震动通知   

  20.         private Button ledButton;// led通知   

  21.         private Button offButton;// 关闭通知   

  22.         NotificationManager notificationManager;   

  23.             /**创建Activity*/   

  24.         public void onCreate(Bundle savedInstanceState){   

  25.             super.onCreate(savedInstanceState);   

  26.             setContentView(R.layout.notification);   

  27.             getComment();   

  28.             registerComment();   

  29.         }   

  30.             /**获取对象*/   

  31.         public void getComment(){   

  32.                 /**获取Notification对象*/   

  33.             notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);   

  34.             textButton = (Button)findViewById(R.id.notificationMessage);   

  35.             soundButton =(Button)findViewById(R.id.notificationSound);   

  36.             vibrateButton = (Button)findViewById(R.id.notificationVibrate);   

  37.             ledButton = (Button)findViewById(R.id.notificationLED);   

  38.             offButton = (Button)findViewById(R.id.offnotification);   

  39.         }   

  40.             /**注册对象*/   

  41.         public void registerComment(){   

  42.             textButton.setOnClickListener(notificationMessage);   

  43.             soundButton.setOnClickListener(notificationSound);   

  44.             vibrateButton.setOnClickListener(notificationVibrate);   

  45.             ledButton.setOnClickListener(notificationLed);   

  46.             offButton.setOnClickListener(notificationOff);   

  47.         }   

  48.         public Button.OnClickListener notificationMessage = new Button.OnClickListener(){   

  49.             public void onClick(View view){   

  50.                 Notification notification = new Notification();// 创建Notification对象   

  51.                 notification.icon = R.drawable.icon;   

  52.                 notification.tickerText = "This is text notication";// 设置通知消息   

  53.                     /**单击通知后的Intent,此例子单击后还是在当前页面*/   

  54.                 PendingIntent intent = PendingIntent   

  55.                     .getActivity(UseNotification.this,   

  56.                             0, new Intent(UseNotification.this,UseNotification.class)   

  57.                             , 0);   

  58.                     /**设置通知消息*/   

  59.                 notification.setLatestEventInfo(UseNotification.this   

  60.                         ,"Notification","Content of Notification Demo",intent);   

  61.                     /**执行通知*/   

  62.                 notificationManager.notify(0, notification);   

  63.             }   

  64.         };   

  65.         public Button.OnClickListener notificationSound = new Button.OnClickListener(){   

  66.             public void onClick(View view){   

  67.                     /**创建通知对象*/   

  68.                 Notification notification = new Notification();   

  69.                     /**获取系统当前声音*/   

  70.                 String ringName = RingtoneManager.getActualDefaultRingtoneUri(   

  71.                         UseNotification.this, RingtoneManager.TYPE_RINGTONE)   

  72.                         .toString();   

  73.                     /**设置系统当前铃声为此通知的铃声*/   

  74.                 notification.sound = Uri.parse(ringName);   

  75.                     /**执行通知*/   

  76.                 notificationManager.notify(0,notification);   

  77.             }   

  78.         };   

  79.             /**震动通知*/   

  80.         public Button.OnClickListener notificationVibrate = new Button.OnClickListener(){   

  81.             public void onClick(View view){   

  82.                 Notification notification = new Notification();// 创建Notification对象   

  83.                 notification.vibrate = new long[] {0, 100, 200, 300};// 设置通知震动模式   

  84.                 notificationManager.notify(0,notification);// 执行通知   

  85.             }   

  86.         };   

  87.             /**LED通知*/   

  88.         public Button.OnClickListener notificationLed = new Button.OnClickListener(){   

  89.             public void onClick(View view){   

  90.                 Notification notification = new Notification();// 创建Notification对象   

  91.                 notification.ledOnMS = 300;// 设置led开始闪光的时间   

  92.                 notification.ledOffMS = 1000;// 设置关闭时的闪光时间   

  93.                 notificationManager.notify(0,notification);// 执行通知   

  94.             }   

  95.         };   

  96.             /**关闭通知*/   

  97.         public Button.OnClickListener notificationOff = new Button.OnClickListener(){   

  98.             public void onClick(View view){   

  99.                 notificationManager.cancel(0);// 关闭通知   

  100.             }   

  101.         };   

  102.     }

6.Alarm 闹钟服务

package lovefang.stadyService;      import android.app.Activity;   import android.os.Bundle;   import android.widget.Button;   import android.view.View;   import android.app.AlarmManager;      import java.util.Calendar;      public class UseAlarmManager extends Activity {           /**创建参数*/       private Button startAlarm;       private Button shutdownAlarm;       private AlarmManager alarm;                  /**创建Activity*/       public void onCreate(Bundle savedInstanceState){           super.onCreate(savedInstanceState);           setContentView(R.layout.usealarmmanager);           getWidget();       }       public void getWidget(){           startAlarm = (Button)findViewById(R.id.startAlarm);           shutdownAlarm = (Button)findViewById(R.id.shutDowntAlarm);           alarm = (AlarmManager)getSystemService(ALARM_SERVICE);// 获取AlarmManager       }       public void registerWidget(){           startAlarm.setOnClickListener(startAlarms);           shutdownAlarm.setOnClickListener(shutdownAlarms);       }           /**启动闹钟*/       public Button.OnClickListener startAlarms = new Button.OnClickListener(){           public void onClick(View view){                   // 设置10秒后出发闹钟               Calendar calendar = Calendar.getInstance();               calendar.setTimeInMillis(System.currentTimeMillis());// 设置calendar的时间               calendar.add(Calendar.SECOND, 10);                              alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), null);           }       };       public Button.OnClickListener shutdownAlarms = new Button.OnClickListener(){           public void onClick(View view){               alarm.cancel(null);           }       };   }

下页将为您介绍获取手机的状态、Vibrator 震动功能等服务的具体内容

7.获取手机的状态

这个功能实现的是获取用户手机的一些定义的信息

package lovefang.stadyService;   /**引入包*/       import android.os.Bundle;       import android.app.Activity;       import android.app.Service;       import android.view.View;       import android.widget.Button;       import android.widget.TextView;       import android.content.ContentResolver;//This class provides applications access to the content model.       import android.telephony.TelephonyManager;       import android.util.Log;   /**获取手机的状态*/       public class UsePhoneState extends Activity{               /**创建参数*/           private ContentResolver cr;           private Button getStateButton;// 用来获取用户的手机状态               /**创建Activity*/           public void onCreate(Bundle savedInstanceState){               super.onCreate(savedInstanceState);               setContentView(R.layout.usephonestate);                              cr = getContentResolver();               Log.v("UsePhonestate","cr = getContentResolver()");               Log.v("UsePhonestate","setContentView");               getStateButton = (Button) findViewById(R.id.button_getphonestate);               Log.v("UsePhonestate","getStateButton");               getStateButton.setOnClickListener(getState);               Log.v("UsePhonestate","getStateButton.setOnClickListener");           }           private Button.OnClickListener getState = new Button.OnClickListener(){               public void onClick(View view){                       /**获得TelephonyManager对象*/                   TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Service.TELEPHONY_SERVICE);                       /**获取电信网络级别*/                   String teleCode = telephonyManager.getNetworkCountryIso();                       /**获取电信网络公司代码*/                   String teleComCode = telephonyManager.getNetworkOperator();                       /**获取电信网络公司名称*/                   String teleComName = telephonyManager.getNetworkOperatorName();                       /**获取行动通信类型*/                   int TypeCode = telephonyManager.getPhoneType();                                      String type = "";                                      switch(TypeCode){                       case TelephonyManager.PHONE_TYPE_NONE:                           type = "PHONE_TYPE_NONE";                           break;                       case TelephonyManager.PHONE_TYPE_GSM:                           type = "PHONE_TYPE_GSM";                           break;                       case TelephonyManager.PHONE_TYPE_CDMA:                           type = "PHONE_TYPE_CDMA";                           break;                   }                       /**获取网络类型*/                   int netTypeCode = telephonyManager.getNetworkType();                   String netType = "NETWORK_TYPE_UNKNOW";                   switch(netTypeCode){                       case TelephonyManager.NETWORK_TYPE_1xRTT:                           netType = "NETWORK_TYPE_1xRTT";                           break;                       case TelephonyManager.NETWORK_TYPE_CDMA:                           netType = "NETWORK_TYPE_CDMA";                           break;                       case TelephonyManager.NETWORK_TYPE_EDGE:                           netType = "NETWORK_TYPE_EDGE";                           break;                       case TelephonyManager.NETWORK_TYPE_EVDO_0:                           netType = "NETWORK_TYPE_EVDO_0";                           break;                       case TelephonyManager.NETWORK_TYPE_EVDO_A:                           netType = "NETWORK_TYPE_EVDO_A";                           break;                       case TelephonyManager.NETWORK_TYPE_GPRS:                           netType = "NETWORK_TYPE_GPRS";                           break;                       case TelephonyManager.NETWORK_TYPE_HSDPA:                           netType = "NETWORK_TYPE_HSDPA";                           break;                       case TelephonyManager.NETWORK_TYPE_HSPA:                           netType = "NETWORK_TYPE_HSPA";                           break;                       case TelephonyManager.NETWORK_TYPE_HSUPA:                           netType = "NETWORK_TYPE_HSUPA";                           break;                       case TelephonyManager.NETWORK_TYPE_IDEN:                           netType = "NETWORK_TYPE_IDEN";                           break;                       case TelephonyManager.NETWORK_TYPE_UMTS:                           netType = "NETWORK_TYPE_UMTS";                           break;                       default:                           break;                   }                                          /**获取漫游状态*/                   boolean roamStatusCode = telephonyManager.isNetworkRoaming();                   String roamStatus = "NOT ROAMINF";                   if(roamStatusCode){                       roamStatus = "ROAMING";                   }                                          /**获取手机***标识*/                   String imei = telephonyManager.getDeviceId();                       /**获取手机IMEI SV*/                   String imeiSV = telephonyManager.getDeviceSoftwareVersion();                       /**获取手机IMSI*/                   String imsi = telephonyManager.getSubscriberId();                                          /**蓝牙服务*/                   String statusCode = android.provider.Settings.System.getString(cr,                           android.provider.Settings.System.BLUETOOTH_ON);                   String bulettothStatus = "";                   if(statusCode.equals("1")){                       bulettothStatus = "ENABLE";                   }else{                       bulettothStatus = "DISABLE";                   }                                          /**飞行模式是否打开*/                   statusCode = android.provider.Settings.System.getString(cr,                           android.provider.Settings.System.AIRPLANE_MODE_ON);                                      String AirplaneStatus = "";                   if(statusCode.equals("1")){                       AirplaneStatus = "ENABLE";                   }else{                       AirplaneStatus = "DISABLE";                   }                                          /**数据漫游模式是否打开*/                   statusCode = android.provider.Settings.System.getString(cr,                           android.provider.Settings.System.DATA_ROAMING);                   String dataRoamStatus = "";                   if(statusCode.equals("1")){                       dataRoamStatus = "ENABLE";                   }else{                       dataRoamStatus = "DISABLE";                   }                   TextView txt = (TextView) findViewById(R.id.text_showphonestate);                   StringBuilder sb = new StringBuilder();                   sb.append("teleCode: "+teleCode+"\n");                   sb.append("teleComCode: "+teleComCode+"\n");                   sb.append("teleComName: "+teleComName+"\n");                   sb.append("type: "+type+"\n");                   sb.append("netType: "+netType+"\n");                   sb.append("roamStatus: "+roamStatus+"\n");                   sb.append("imei: "+imei+"\n");                   sb.append("imeiSV: "+imeiSV+"\n");                   sb.append("imsi: "+imsi+"\n");                   sb.append("bulettothStatus: "+bulettothStatus+"\n");                   sb.append("AirplaneStatus: "+AirplaneStatus+"\n");                   sb.append("dataRoamStatus: "+dataRoamStatus+"\n");                                      txt.setText(sb.toString());               }           };       }

8.Vibrator 震动功能

实现对手机震动的管理

package lovefang.stadyService;   /***/       import android.os.Bundle;       import android.os.Vibrator;       import android.app.Activity;       import android.view.View;       import android.content.Context;       import android.widget.Button;   /**如何实现手机的震动提示Vibrator*/       public class UseVibrator extends Activity{               /***/           private Button vibrator_1_Button;           private Button vibrator_2_Button;           private Button vibrator_3_Button;           private Vibrator vibrator;               /***/           public void onCreate(Bundle savedInstanceState){               super.onCreate(savedInstanceState);               setContentView(R.layout.use_vibrator);               vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);               getWidget();               registerWidget();           }                      public void getWidget(){               vibrator_1_Button = (Button) findViewById(R.id.button_vibrator_1);               vibrator_2_Button = (Button) findViewById(R.id.button_vibrator_2);               vibrator_3_Button = (Button) findViewById(R.id.button_vibrator_3);           }                      public void registerWidget(){               vibrator_1_Button.setOnClickListener(vibrator_1);               vibrator_2_Button.setOnClickListener(vibrator_2);               vibrator_3_Button.setOnClickListener(vibrator_3);           }               /**震动一次*/           public Button.OnClickListener vibrator_1 = new Button.OnClickListener(){               public void onClick(View view){                       /**long参数数组里大参数的含义*/                       /*****个参数表示等待100毫秒后开始震动*/                       /**第二个参数表示震动100毫秒后停止震动*/                   vibrator.vibrate(new long[]{100,100}, 0);               }           };               /**震动两次*/           public Button.OnClickListener vibrator_2 = new Button.OnClickListener(){               public void onClick(View view){                   vibrator.vibrate(new long[]{1000,3000,1000,3000}, 0);               }           };               /**震动三次*/           public Button.OnClickListener vibrator_3 = new Button.OnClickListener(){               public void onClick(View view){                   vibrator.vibrate(new long[]{1000,1000,1000,2000,1000,300}, 0);               }           };       }

到此,相信大家对“Android中的服务怎么实现”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI