android 系统服务

android常用的一些服务 demo源码

分类: Android Demo 108人阅读 评论(0) 收藏 举报
Android

今天在网站看一系列例子。太棒了。。。

我收藏了哦。

实现了Android中常见的许多服务,下面是实现的截图

android 系统服务_第1张图片

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


1.MainActivity--主界面

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

[java] view plain copy
  1. package lovefang.stadyService;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.view.View;
  6. import android.content.Intent;
  7. import android.util.Log;
  8. /**这是使用后台服务的学习例子*/
  9. public class MainStadyServics extends Activity {
  10. /**参数设置*/
  11. Button startServiceButton;// 启动服务按钮
  12. Button shutDownServiceButton;// 关闭服务按钮
  13. Button startBindServiceButton;// 启动绑定服务按钮
  14. Button sendBroadcast;// 使用广播
  15. Button notificationButton;// 使用通知功能
  16. Button alarmButton;// 使用闹钟
  17. Button handlerButton;// 使用handler
  18. Button asyncButton;// 使用异步加载
  19. Button phoneStateButton;// 查看手机状态
  20. Button callphoneButton;// 拨打电话
  21. Button vibratorButton;// 使用震动
  22. CountService countService;
  23. @Override
  24. public void onCreate(Bundle savedInstanceState) {
  25. super.onCreate(savedInstanceState);
  26. Log.v("MainStadyServics", "setContentView");
  27. setContentView(R.layout.main);
  28. getWidget();
  29. regiestListener();
  30. }
  31. /**获得组件*/
  32. public void getWidget(){
  33. startServiceButton = (Button)findViewById(R.id.startServerButton);
  34. startBindServiceButton = (Button)findViewById(R.id.startBindServerButton);
  35. shutDownServiceButton = (Button)findViewById(R.id.sutdownServerButton);
  36. sendBroadcast = (Button)findViewById(R.id.sendBroadcast);
  37. notificationButton = (Button)findViewById(R.id.notification);
  38. alarmButton = (Button)findViewById(R.id.alarm);
  39. handlerButton = (Button)findViewById(R.id.handler);
  40. asyncButton = (Button)findViewById(R.id.async);
  41. phoneStateButton = (Button) findViewById(R.id.phonestate);
  42. callphoneButton = (Button) findViewById(R.id.callphone);
  43. vibratorButton = (Button) findViewById(R.id.vibrator);
  44. }
  45. /**为按钮添加监听*/
  46. public void regiestListener(){
  47. startServiceButton.setOnClickListener(startService);
  48. shutDownServiceButton.setOnClickListener(shutdownService);
  49. startBindServiceButton.setOnClickListener(startBinderService);
  50. sendBroadcast.setOnClickListener(broadcastReceiver);
  51. notificationButton.setOnClickListener(notification);
  52. alarmButton.setOnClickListener(startAlarm);
  53. handlerButton.setOnClickListener(handler);
  54. asyncButton.setOnClickListener(async);
  55. phoneStateButton.setOnClickListener(phonestate);
  56. callphoneButton.setOnClickListener(callphoneEvent);
  57. vibratorButton.setOnClickListener(vibrator);
  58. }
  59. /**启动服务的事件监听*/
  60. public Button.OnClickListener startService = new Button.OnClickListener(){
  61. public void onClick(View view){
  62. /**单击按钮时启动服务*/
  63. Intent intent = new Intent(MainStadyServics.this,CountService.class);
  64. startService(intent);
  65. Log.v("MainStadyServics", "start Service");
  66. }
  67. };
  68. /**关闭服务*/
  69. public Button.OnClickListener shutdownService = new Button.OnClickListener(){
  70. public void onClick(View view){
  71. /**单击按钮时启动服务*/
  72. Intent intent = new Intent(MainStadyServics.this,CountService.class);
  73. /**退出Activity是,停止服务*/
  74. stopService(intent);
  75. Log.v("MainStadyServics", "shutDown serveice");
  76. }
  77. };
  78. /**打开绑定服务的Activity*/
  79. public Button.OnClickListener startBinderService = new Button.OnClickListener(){
  80. public void onClick(View view){
  81. /**单击按钮时启动服务*/
  82. Intent intent = new Intent(MainStadyServics.this,UseBrider.class);
  83. startActivity(intent);
  84. Log.v("MainStadyServics", "start Binder Service");
  85. }
  86. };
  87. /**打开广播学习的按钮*/
  88. public Button.OnClickListener broadcastReceiver = new Button.OnClickListener(){
  89. public void onClick(View view){
  90. Intent intent = new Intent(MainStadyServics.this,UseBroadcast.class);
  91. startActivity(intent);
  92. Log.v("MainStadyServics","start broadcast");
  93. }
  94. };
  95. /**打开通知*/
  96. public Button.OnClickListener notification = new Button.OnClickListener(){
  97. public void onClick(View view){
  98. Intent intent = new Intent(MainStadyServics.this, UseNotification.class);
  99. startActivity(intent);
  100. Log.v("MainStadyService ","start Notification");
  101. }
  102. };
  103. /**使用闹钟*/
  104. public Button.OnClickListener startAlarm = new Button.OnClickListener(){
  105. public void onClick(View view){
  106. Intent intent = new Intent(MainStadyServics.this, UseAlarmManager.class);
  107. startActivity(intent);
  108. Log.v("MainStadyService ","start alarm");
  109. }
  110. };
  111. public Button.OnClickListener handler= new Button.OnClickListener(){
  112. public void onClick(View view){
  113. Intent intent = new Intent(MainStadyServics.this, UseHandleMessage.class);
  114. startActivity(intent);
  115. Log.v("MainStadyService ","start handle");
  116. }
  117. };
  118. public Button.OnClickListener async= new Button.OnClickListener(){
  119. public void onClick(View view){
  120. Intent intent = new Intent(MainStadyServics.this, UseAsyncTask.class);
  121. startActivity(intent);
  122. Log.v("MainStadyService ","start handle");
  123. }
  124. };
  125. public Button.OnClickListener phonestate= new Button.OnClickListener(){
  126. public void onClick(View view){
  127. Intent intent = new Intent(MainStadyServics.this, UsePhoneState.class);
  128. startActivity(intent);
  129. Log.v("MainStadyService ","start phonestate");
  130. }
  131. };
  132. public Button.OnClickListener callphoneEvent= new Button.OnClickListener(){
  133. public void onClick(View view){
  134. Intent intent = new Intent(MainStadyServics.this, UseActionCall.class);
  135. startActivity(intent);
  136. Log.v("MainStadyService ","start callphone");
  137. }
  138. };
  139. public Button.OnClickListener vibrator= new Button.OnClickListener(){
  140. public void onClick(View view){
  141. Intent intent = new Intent(MainStadyServics.this, UseVibrator.class);
  142. startActivity(intent);
  143. Log.v("MainStadyService ","start callphone");
  144. }
  145. };
  146. /***/
  147. protected void onDestroy(){
  148. super.onDestroy();
  149. Intent intent = new Intent(MainStadyServics.this,CountService.class);
  150. /**退出Activity是,停止服务*/
  151. stopService(intent);
  152. }
  153. }

2.启动服务按钮

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

代码如下:

[java] view plain copy
  1. package lovefang.stadyService;
  2. /**引入包*/
  3. import android.app.Service;// 服务的类
  4. import android.os.IBinder;
  5. import android.os.Binder;
  6. import android.content.Intent;
  7. import android.util.Log;
  8. /**计数的服务*/
  9. public class CountService extends Service{
  10. /**创建参数*/
  11. boolean threadDisable ;
  12. int count;
  13. public IBinder onBind(Intent intent){
  14. return null;
  15. }
  16. public void onCreate(){
  17. super.onCreate();
  18. /**创建一个线程,每秒计数器加一,并在控制台进行Log输出*/
  19. new Thread(new Runnable(){
  20. public void run(){
  21. while(!threadDisable){
  22. try{
  23. Thread.sleep(1000);
  24. }catch(InterruptedException e){
  25. }
  26. count++;
  27. Log.v("CountService","Count is"+count);
  28. }
  29. }
  30. }).start();
  31. }
  32. public void onDestroy(){
  33. super.onDestroy();
  34. /**服务停止时,终止计数进程*/
  35. this.threadDisable = true;
  36. }
  37. public int getConunt(){
  38. return count;
  39. }
  40. class ServiceBinder extends Binder{
  41. public CountService getService(){
  42. return CountService.this;
  43. }
  44. }
  45. }

3.绑定服务

服务有两种实现的方法:

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

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

下面是实现的代码:

[java] view plain copy
  1. package lovefang.stadyService;
  2. /**引入包*/
  3. import android.app.Activity;
  4. import android.content.ComponentName;
  5. import android.content.Context;
  6. import android.content.Intent;
  7. import android.content.ServiceConnection;
  8. import android.os.Bundle;
  9. import android.os.IBinder;
  10. import android.util.Log;
  11. /**通过bindService和unBindSerivce的方式启动和结束服务*/
  12. public class UseBrider extends Activity {
  13. /**参数设置*/
  14. CountService countService;
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(new UseBriderFace(this));
  19. Intent intent = new Intent(UseBrider.this,CountService.class);
  20. /**进入Activity开始服务*/
  21. bindService(intent, conn, Context.BIND_AUTO_CREATE);
  22. }
  23. private ServiceConnection conn = new ServiceConnection(){
  24. /**获取服务对象时的操作*/
  25. public void onServiceConnected(ComponentName name, IBinder service) {
  26. // TODO Auto-generated method stub
  27. countService = ((CountService.ServiceBinder)service).getService();
  28. }
  29. /**无法获取到服务对象时的操作*/
  30. public void onServiceDisconnected(ComponentName name) {
  31. // TODO Auto-generated method stub
  32. countService =null;
  33. }
  34. };
  35. protected void onDestroy(){
  36. super.onDestroy();
  37. this.unbindService(conn);
  38. Log.v("MainStadyServics", "out");
  39. }
  40. }

4.发送广播

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

实现的代码如下:

4.1 打开广播服务

[java] view plain copy
  1. package lovefang.stadyService;
  2. /**引入包*/
  3. import android.view.View;
  4. import android.os.Bundle;
  5. import android.app.Activity;
  6. import android.content.Intent;
  7. import android.widget.Button;
  8. /**使用Broadcast,这是一个发送广播的类*/
  9. public class UseBroadcast extends Activity{
  10. /**创建参数*/
  11. private Button sendBroadcast;
  12. /**创建Activity*/
  13. public void onCreate(Bundle savedInstanceState){
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.broadcast);// 使用布局文件
  16. getView();
  17. sendBroadcast.setOnClickListener(sendBroadcastClick);// 添加事件监听
  18. }
  19. public void getView(){
  20. sendBroadcast = (Button)findViewById(R.id.sendBroadcast);
  21. }
  22. /**创建事件监听*/
  23. public Button.OnClickListener sendBroadcastClick = new Button.OnClickListener(){
  24. public void onClick(View view){
  25. Intent intent = new Intent();// 创建意图
  26. intent.putExtra("CONTENT", "This is a Braodcast demo");// 设置广播的内容
  27. intent.setAction("lovefang.stadyService");// 设置广播的Action
  28. sendBroadcast(intent);
  29. }
  30. };
  31. }


4.2 处理广播消息

[java] view plain copy
  1. package lovefang.stadyService;
  2. /***/
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.util.Log;
  7. /**这是一个接收广播的类*/
  8. public class UseBroadcastReceiver extends BroadcastReceiver{
  9. public void onReceive(Context context, Intent intent){
  10. Log.v("UseBroadcastReceiver", "I get a message");
  11. }
  12. }

5.Notification

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

实现的代码如下:

[java] view plain copy
  1. package lovefang.stadyService;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.app.Notification;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.net.Uri;
  9. import android.media.RingtoneManager;
  10. import android.widget.Button;
  11. import android.view.View;
  12. /**使用notification*/
  13. public class UseNotification extends Activity {
  14. /**创建组件*/
  15. private Button textButton;
  16. private Button soundButton;// 声音通知
  17. private Button vibrateButton;// 震动通知
  18. private Button ledButton;// led通知
  19. private Button offButton;// 关闭通知
  20. NotificationManager notificationManager;
  21. /**创建Activity*/
  22. public void onCreate(Bundle savedInstanceState){
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.notification);
  25. getComment();
  26. registerComment();
  27. }
  28. /**获取对象*/
  29. public void getComment(){
  30. /**获取Notification对象*/
  31. notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE);
  32. textButton = (Button)findViewById(R.id.notificationMessage);
  33. soundButton =(Button)findViewById(R.id.notificationSound);
  34. vibrateButton = (Button)findViewById(R.id.notificationVibrate);
  35. ledButton = (Button)findViewById(R.id.notificationLED);
  36. offButton = (Button)findViewById(R.id.offnotification);
  37. }
  38. /**注册对象*/
  39. public void registerComment(){
  40. textButton.setOnClickListener(notificationMessage);
  41. soundButton.setOnClickListener(notificationSound);
  42. vibrateButton.setOnClickListener(notificationVibrate);
  43. ledButton.setOnClickListener(notificationLed);
  44. offButton.setOnClickListener(notificationOff);
  45. }
  46. public Button.OnClickListener notificationMessage = new Button.OnClickListener(){
  47. public void onClick(View view){
  48. Notification notification = new Notification();// 创建Notification对象
  49. notification.icon = R.drawable.icon;
  50. notification.tickerText = "This is text notication";// 设置通知消息
  51. /**单击通知后的Intent,此例子单击后还是在当前页面*/
  52. PendingIntent intent = PendingIntent
  53. .getActivity(UseNotification.this,
  54. 0, new Intent(UseNotification.this,UseNotification.class)
  55. , 0);
  56. /**设置通知消息*/
  57. notification.setLatestEventInfo(UseNotification.this
  58. ,"Notification","Content of Notification Demo",intent);
  59. /**执行通知*/
  60. notificationManager.notify(0, notification);
  61. }
  62. };
  63. public Button.OnClickListener notificationSound = new Button.OnClickListener(){
  64. public void onClick(View view){
  65. /**创建通知对象*/
  66. Notification notification = new Notification();
  67. /**获取系统当前声音*/
  68. String ringName = RingtoneManager.getActualDefaultRingtoneUri(
  69. UseNotification.this, RingtoneManager.TYPE_RINGTONE)
  70. .toString();
  71. /**设置系统当前铃声为此通知的铃声*/
  72. notification.sound = Uri.parse(ringName);
  73. /**执行通知*/
  74. notificationManager.notify(0,notification);
  75. }
  76. };
  77. /**震动通知-七七八八网*/
  78. public Button.OnClickListener notificationVibrate = new Button.OnClickListener(){
  79. public void onClick(View view){
  80. Notification notification = new Notification();// 创建Notification对象
  81. notification.vibrate = new long[] {0, 100, 200, 300};// 设置通知震动模式
  82. notificationManager.notify(0,notification);// 执行通知
  83. }
  84. };
  85. /**LED通知*/
  86. public Button.OnClickListener notificationLed = new Button.OnClickListener(){
  87. public void onClick(View view){
  88. Notification notification = new Notification();// 创建Notification对象
  89. notification.ledOnMS = 300;// 设置led开始闪光的时间
  90. notification.ledOffMS = 1000;// 设置关闭时的闪光时间
  91. notificationManager.notify(0,notification);// 执行通知
  92. }
  93. };
  94. /**关闭通知*/
  95. public Button.OnClickListener notificationOff = new Button.OnClickListener(){
  96. public void onClick(View view){
  97. notificationManager.cancel(0);// 关闭通知
  98. }
  99. };
  100. }

6.Alarm

闹钟服务

[java] view plain copy
  1. package lovefang.stadyService;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.widget.Button;
  5. import android.view.View;
  6. import android.app.AlarmManager;
  7. import java.util.Calendar;
  8. public class UseAlarmManager extends Activity {
  9. /**创建参数*/
  10. private Button startAlarm;
  11. private Button shutdownAlarm;
  12. private AlarmManager alarm;
  13. /**创建Activity*/
  14. public void onCreate(Bundle savedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.usealarmmanager);
  17. getWidget();
  18. }
  19. public void getWidget(){
  20. startAlarm = (Button)findViewById(R.id.startAlarm);
  21. shutdownAlarm = (Button)findViewById(R.id.shutDowntAlarm);
  22. alarm = (AlarmManager)getSystemService(ALARM_SERVICE);// 获取AlarmManager
  23. }
  24. public void registerWidget(){
  25. startAlarm.setOnClickListener(startAlarms);
  26. shutdownAlarm.setOnClickListener(shutdownAlarms);
  27. }
  28. /**启动闹钟*/
  29. public Button.OnClickListener startAlarms = new Button.OnClickListener(){
  30. public void onClick(View view){
  31. // 设置10秒后出发闹钟
  32. Calendar calendar = Calendar.getInstance();
  33. calendar.setTimeInMillis(System.currentTimeMillis());// 设置calendar的时间
  34. calendar.add(Calendar.SECOND, 10);
  35. alarm.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), null);
  36. }
  37. };
  38. public Button.OnClickListener shutdownAlarms = new Button.OnClickListener(){
  39. public void onClick(View view){
  40. alarm.cancel(null);
  41. }
  42. };
  43. }

7.获取手机的状态

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

[java] view plain copy
  1. package lovefang.stadyService;
  2. /**引入包*/
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.app.Service;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.TextView;
  9. import android.content.ContentResolver;//This class provides applications access to the content model.
  10. import android.telephony.TelephonyManager;
  11. import android.util.Log;
  12. /**尖锋网-获取手机的状态*/
  13. public class UsePhoneState extends Activity{
  14. /**创建参数*/
  15. private ContentResolver cr;
  16. private Button getStateButton;// 用来获取用户的手机状态
  17. /**创建Activity*/
  18. public void onCreate(Bundle savedInstanceState){
  19. super.onCreate(savedInstanceState);
  20. setContentView(R.layout.usephonestate);
  21. cr = getContentResolver();
  22. Log.v("UsePhonestate","cr = getContentResolver()");
  23. Log.v("UsePhonestate","setContentView");
  24. getStateButton = (Button) findViewById(R.id.button_getphonestate);
  25. Log.v("UsePhonestate","getStateButton");
  26. getStateButton.setOnClickListener(getState);
  27. Log.v("UsePhonestate","getStateButton.setOnClickListener");
  28. }
  29. private Button.OnClickListener getState = new Button.OnClickListener(){
  30. public void onClick(View view){
  31. /**获得TelephonyManager对象*/
  32. TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Service.TELEPHONY_SERVICE);
  33. /**获取电信网络级别*/
  34. String teleCode = telephonyManager.getNetworkCountryIso();
  35. /**获取电信网络公司代码*/
  36. String teleComCode = telephonyManager.getNetworkOperator();
  37. /**获取电信网络公司名称*/
  38. String teleComName = telephonyManager.getNetworkOperatorName();
  39. /**获取行动通信类型*/
  40. int TypeCode = telephonyManager.getPhoneType();
  41. String type = "";
  42. switch(TypeCode){
  43. case TelephonyManager.PHONE_TYPE_NONE:
  44. type = "PHONE_TYPE_NONE";
  45. break;
  46. case TelephonyManager.PHONE_TYPE_GSM:
  47. type = "PHONE_TYPE_GSM";
  48. break;
  49. case TelephonyManager.PHONE_TYPE_CDMA:
  50. type = "PHONE_TYPE_CDMA";
  51. break;
  52. }
  53. /**获取网络类型*/
  54. int netTypeCode = telephonyManager.getNetworkType();
  55. String netType = "NETWORK_TYPE_UNKNOW";
  56. switch(netTypeCode){
  57. case TelephonyManager.NETWORK_TYPE_1xRTT:
  58. netType = "NETWORK_TYPE_1xRTT";
  59. break;
  60. case TelephonyManager.NETWORK_TYPE_CDMA:
  61. netType = "NETWORK_TYPE_CDMA";
  62. break;
  63. case TelephonyManager.NETWORK_TYPE_EDGE:
  64. netType = "NETWORK_TYPE_EDGE";
  65. break;
  66. case TelephonyManager.NETWORK_TYPE_EVDO_0:
  67. netType = "NETWORK_TYPE_EVDO_0";
  68. break;
  69. case TelephonyManager.NETWORK_TYPE_EVDO_A:
  70. netType = "NETWORK_TYPE_EVDO_A";
  71. break;
  72. case TelephonyManager.NETWORK_TYPE_GPRS:
  73. netType = "NETWORK_TYPE_GPRS";
  74. break;
  75. case TelephonyManager.NETWORK_TYPE_HSDPA:
  76. netType = "NETWORK_TYPE_HSDPA";
  77. break;
  78. case TelephonyManager.NETWORK_TYPE_HSPA:
  79. netType = "NETWORK_TYPE_HSPA";
  80. break;
  81. case TelephonyManager.NETWORK_TYPE_HSUPA:
  82. netType = "NETWORK_TYPE_HSUPA";
  83. break;
  84. case TelephonyManager.NETWORK_TYPE_IDEN:
  85. netType = "NETWORK_TYPE_IDEN";
  86. break;
  87. case TelephonyManager.NETWORK_TYPE_UMTS:
  88. netType = "NETWORK_TYPE_UMTS";
  89. break;
  90. default:
  91. break;
  92. }
  93. /**获取漫游状态*/
  94. boolean roamStatusCode = telephonyManager.isNetworkRoaming();
  95. String roamStatus = "NOT ROAMINF";
  96. if(roamStatusCode){
  97. roamStatus = "ROAMING";
  98. }
  99. /**http://www.jfong.cn/ -获取手机唯一标识*/
  100. String imei = telephonyManager.getDeviceId();
  101. /**获取手机IMEI SV*/
  102. String imeiSV = telephonyManager.getDeviceSoftwareVersion();
  103. /**获取手机IMSI*/
  104. String imsi = telephonyManager.getSubscriberId();
  105. /**http://www.qi788.com/ -蓝牙服务*/
  106. String statusCode = android.provider.Settings.System.getString(cr,
  107. android.provider.Settings.System.BLUETOOTH_ON);
  108. String bulettothStatus = "";
  109. if(statusCode.equals("1")){
  110. bulettothStatus = "ENABLE";
  111. }else{
  112. bulettothStatus = "DISABLE";
  113. }
  114. /**飞行模式是否打开*/
  115. statusCode = android.provider.Settings.System.getString(cr,
  116. android.provider.Settings.System.AIRPLANE_MODE_ON);
  117. String AirplaneStatus = "";
  118. if(statusCode.equals("1")){
  119. AirplaneStatus = "ENABLE";
  120. }else{
  121. AirplaneStatus = "DISABLE";
  122. }
  123. /**数据漫游模式是否打开*/
  124. statusCode = android.provider.Settings.System.getString(cr,
  125. android.provider.Settings.System.DATA_ROAMING);
  126. String dataRoamStatus = "";
  127. if(statusCode.equals("1")){
  128. dataRoamStatus = "ENABLE";
  129. }else{
  130. dataRoamStatus = "DISABLE";
  131. }
  132. TextView txt = (TextView) findViewById(R.id.text_showphonestate);
  133. StringBuilder sb = new StringBuilder();
  134. sb.append("teleCode: "+teleCode+"\n");
  135. sb.append("teleComCode: "+teleComCode+"\n");
  136. sb.append("teleComName: "+teleComName+"\n");
  137. sb.append("type: "+type+"\n");
  138. sb.append("netType: "+netType+"\n");
  139. sb.append("roamStatus: "+roamStatus+"\n");
  140. sb.append("imei: "+imei+"\n");
  141. sb.append("imeiSV: "+imeiSV+"\n");
  142. sb.append("imsi: "+imsi+"\n");
  143. sb.append("bulettothStatus: "+bulettothStatus+"\n");
  144. sb.append("AirplaneStatus: "+AirplaneStatus+"\n");
  145. sb.append("dataRoamStatus: "+dataRoamStatus+"\n");
  146. txt.setText(sb.toString());
  147. }
  148. };
  149. }

8.Vibrator

震动功能,实现对手机震动的管理

[java] view plain copy
  1. package lovefang.stadyService;
  2. /***/
  3. import android.os.Bundle;
  4. import android.os.Vibrator;
  5. import android.app.Activity;
  6. import android.view.View;
  7. import android.content.Context;
  8. import android.widget.Button;
  9. /**如何实现手机的震动提示Vibrator*/
  10. public class UseVibrator extends Activity{
  11. /***/
  12. private Button vibrator_1_Button;
  13. private Button vibrator_2_Button;
  14. private Button vibrator_3_Button;
  15. private Vibrator vibrator;
  16. /***/
  17. public void onCreate(Bundle savedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.use_vibrator);
  20. vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
  21. getWidget();
  22. registerWidget();
  23. }
  24. public void getWidget(){
  25. vibrator_1_Button = (Button) findViewById(R.id.button_vibrator_1);
  26. vibrator_2_Button = (Button) findViewById(R.id.button_vibrator_2);
  27. vibrator_3_Button = (Button) findViewById(R.id.button_vibrator_3);
  28. }
  29. public void registerWidget(){
  30. vibrator_1_Button.setOnClickListener(vibrator_1);
  31. vibrator_2_Button.setOnClickListener(vibrator_2);
  32. vibrator_3_Button.setOnClickListener(vibrator_3);
  33. }
  34. /**震动一次*/
  35. public Button.OnClickListener vibrator_1 = new Button.OnClickListener(){
  36. public void onClick(View view){
  37. /**long参数数组里大参数的含义*/
  38. /**第一个参数表示等待100毫秒后开始震动*/
  39. /**第二个参数表示震动100毫秒后停止震动*/
  40. vibrator.vibrate(new long[]{100,100}, 0);
  41. }
  42. };
  43. /**震动两次*/
  44. public Button.OnClickListener vibrator_2 = new Button.OnClickListener(){
  45. public void onClick(View view){
  46. vibrator.vibrate(new long[]{1000,3000,1000,3000}, 0);
  47. }
  48. };
  49. /**震动三次*/
  50. public Button.OnClickListener vibrator_3 = new Button.OnClickListener(){
  51. public void onClick(View view){
  52. vibrator.vibrate(new long[]{1000,1000,1000,2000,1000,300}, 0);
  53. }
  54. };
  55. }

你可能感兴趣的:(android,demo)