`
20386053
  • 浏览: 433101 次
文章分类
社区版块
存档分类
最新评论

Android常用的一些服务demo源码

 
阅读更多

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

我收藏了哦。

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


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

 

1.MainActivity--主界面

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

 

[java] view plaincopy
  1. packagelovefang.stadyService;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.widget.Button;
  5. importandroid.view.View;
  6. importandroid.content.Intent;
  7. importandroid.util.Log;
  8. /**这是使用后台服务的学习例子*/
  9. publicclassMainStadyServicsextendsActivity{
  10. /**参数设置*/
  11. ButtonstartServiceButton;//启动服务按钮
  12. ButtonshutDownServiceButton;//关闭服务按钮
  13. ButtonstartBindServiceButton;//启动绑定服务按钮
  14. ButtonsendBroadcast;//使用广播
  15. ButtonnotificationButton;//使用通知功能
  16. ButtonalarmButton;//使用闹钟
  17. ButtonhandlerButton;//使用handler
  18. ButtonasyncButton;//使用异步加载
  19. ButtonphoneStateButton;//查看手机状态
  20. ButtoncallphoneButton;//拨打电话
  21. ButtonvibratorButton;//使用震动
  22. CountServicecountService;
  23. @Override
  24. publicvoidonCreate(BundlesavedInstanceState){
  25. super.onCreate(savedInstanceState);
  26. Log.v("MainStadyServics","setContentView");
  27. setContentView(R.layout.main);
  28. getWidget();
  29. regiestListener();
  30. }
  31. /**获得组件*/
  32. publicvoidgetWidget(){
  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. publicvoidregiestListener(){
  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. publicButton.OnClickListenerstartService=newButton.OnClickListener(){
  61. publicvoidonClick(Viewview){
  62. /**单击按钮时启动服务*/
  63. Intentintent=newIntent(MainStadyServics.this,CountService.class);
  64. startService(intent);
  65. Log.v("MainStadyServics","startService");
  66. }
  67. };
  68. /**关闭服务*/
  69. publicButton.OnClickListenershutdownService=newButton.OnClickListener(){
  70. publicvoidonClick(Viewview){
  71. /**单击按钮时启动服务*/
  72. Intentintent=newIntent(MainStadyServics.this,CountService.class);
  73. /**退出Activity是,停止服务*/
  74. stopService(intent);
  75. Log.v("MainStadyServics","shutDownserveice");
  76. }
  77. };
  78. /**打开绑定服务的Activity*/
  79. publicButton.OnClickListenerstartBinderService=newButton.OnClickListener(){
  80. publicvoidonClick(Viewview){
  81. /**单击按钮时启动服务*/
  82. Intentintent=newIntent(MainStadyServics.this,UseBrider.class);
  83. startActivity(intent);
  84. Log.v("MainStadyServics","startBinderService");
  85. }
  86. };
  87. /**打开广播学习的按钮*/
  88. publicButton.OnClickListenerbroadcastReceiver=newButton.OnClickListener(){
  89. publicvoidonClick(Viewview){
  90. Intentintent=newIntent(MainStadyServics.this,UseBroadcast.class);
  91. startActivity(intent);
  92. Log.v("MainStadyServics","startbroadcast");
  93. }
  94. };
  95. /**打开通知*/
  96. publicButton.OnClickListenernotification=newButton.OnClickListener(){
  97. publicvoidonClick(Viewview){
  98. Intentintent=newIntent(MainStadyServics.this,UseNotification.class);
  99. startActivity(intent);
  100. Log.v("MainStadyService","startNotification");
  101. }
  102. };
  103. /**使用闹钟*/
  104. publicButton.OnClickListenerstartAlarm=newButton.OnClickListener(){
  105. publicvoidonClick(Viewview){
  106. Intentintent=newIntent(MainStadyServics.this,UseAlarmManager.class);
  107. startActivity(intent);
  108. Log.v("MainStadyService","startalarm");
  109. }
  110. };
  111. publicButton.OnClickListenerhandler=newButton.OnClickListener(){
  112. publicvoidonClick(Viewview){
  113. Intentintent=newIntent(MainStadyServics.this,UseHandleMessage.class);
  114. startActivity(intent);
  115. Log.v("MainStadyService","starthandle");
  116. }
  117. };
  118. publicButton.OnClickListenerasync=newButton.OnClickListener(){
  119. publicvoidonClick(Viewview){
  120. Intentintent=newIntent(MainStadyServics.this,UseAsyncTask.class);
  121. startActivity(intent);
  122. Log.v("MainStadyService","starthandle");
  123. }
  124. };
  125. publicButton.OnClickListenerphonestate=newButton.OnClickListener(){
  126. publicvoidonClick(Viewview){
  127. Intentintent=newIntent(MainStadyServics.this,UsePhoneState.class);
  128. startActivity(intent);
  129. Log.v("MainStadyService","startphonestate");
  130. }
  131. };
  132. publicButton.OnClickListenercallphoneEvent=newButton.OnClickListener(){
  133. publicvoidonClick(Viewview){
  134. Intentintent=newIntent(MainStadyServics.this,UseActionCall.class);
  135. startActivity(intent);
  136. Log.v("MainStadyService","startcallphone");
  137. }
  138. };
  139. publicButton.OnClickListenervibrator=newButton.OnClickListener(){
  140. publicvoidonClick(Viewview){
  141. Intentintent=newIntent(MainStadyServics.this,UseVibrator.class);
  142. startActivity(intent);
  143. Log.v("MainStadyService","startcallphone");
  144. }
  145. };
  146. /***/
  147. protectedvoidonDestroy(){
  148. super.onDestroy();
  149. Intentintent=newIntent(MainStadyServics.this,CountService.class);
  150. /**退出Activity是,停止服务*/
  151. stopService(intent);
  152. }
  153. }


2.启动服务按钮

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

代码如下:

 

[java] view plaincopy
  1. packagelovefang.stadyService;
  2. /**引入包*/
  3. importandroid.app.Service;//服务的类
  4. importandroid.os.IBinder;
  5. importandroid.os.Binder;
  6. importandroid.content.Intent;
  7. importandroid.util.Log;
  8. /**计数的服务*/
  9. publicclassCountServiceextendsService{
  10. /**创建参数*/
  11. booleanthreadDisable;
  12. intcount;
  13. publicIBinderonBind(Intentintent){
  14. returnnull;
  15. }
  16. publicvoidonCreate(){
  17. super.onCreate();
  18. /**创建一个线程,每秒计数器加一,并在控制台进行Log输出*/
  19. newThread(newRunnable(){
  20. publicvoidrun(){
  21. while(!threadDisable){
  22. try{
  23. Thread.sleep(1000);
  24. }catch(InterruptedExceptione){
  25. }
  26. count++;
  27. Log.v("CountService","Countis"+count);
  28. }
  29. }
  30. }).start();
  31. }
  32. publicvoidonDestroy(){
  33. super.onDestroy();
  34. /**服务停止时,终止计数进程*/
  35. this.threadDisable=true;
  36. }
  37. publicintgetConunt(){
  38. returncount;
  39. }
  40. classServiceBinderextendsBinder{
  41. publicCountServicegetService(){
  42. returnCountService.this;
  43. }
  44. }
  45. }


3.绑定服务

服务有两种实现的方法:

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

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

下面是实现的代码:

 

[java] view plaincopy
  1. packagelovefang.stadyService;
  2. /**引入包*/
  3. importandroid.app.Activity;
  4. importandroid.content.ComponentName;
  5. importandroid.content.Context;
  6. importandroid.content.Intent;
  7. importandroid.content.ServiceConnection;
  8. importandroid.os.Bundle;
  9. importandroid.os.IBinder;
  10. importandroid.util.Log;
  11. /**通过bindService和unBindSerivce的方式启动和结束服务*/
  12. publicclassUseBriderextendsActivity{
  13. /**参数设置*/
  14. CountServicecountService;
  15. @Override
  16. publicvoidonCreate(BundlesavedInstanceState){
  17. super.onCreate(savedInstanceState);
  18. setContentView(newUseBriderFace(this));
  19. Intentintent=newIntent(UseBrider.this,CountService.class);
  20. /**进入Activity开始服务*/
  21. bindService(intent,conn,Context.BIND_AUTO_CREATE);
  22. }
  23. privateServiceConnectionconn=newServiceConnection(){
  24. /**获取服务对象时的操作*/
  25. publicvoidonServiceConnected(ComponentNamename,IBinderservice){
  26. //TODOAuto-generatedmethodstub
  27. countService=((CountService.ServiceBinder)service).getService();
  28. }
  29. /**无法获取到服务对象时的操作*/
  30. publicvoidonServiceDisconnected(ComponentNamename){
  31. //TODOAuto-generatedmethodstub
  32. countService=null;
  33. }
  34. };
  35. protectedvoidonDestroy(){
  36. super.onDestroy();
  37. this.unbindService(conn);
  38. Log.v("MainStadyServics","out");
  39. }
  40. }


4.发送广播

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

实现的代码如下:

4.1 打开广播服务

 

[java] view plaincopy
  1. packagelovefang.stadyService;
  2. /**引入包*/
  3. importandroid.view.View;
  4. importandroid.os.Bundle;
  5. importandroid.app.Activity;
  6. importandroid.content.Intent;
  7. importandroid.widget.Button;
  8. /**使用Broadcast,这是一个发送广播的类*/
  9. publicclassUseBroadcastextendsActivity{
  10. /**创建参数*/
  11. privateButtonsendBroadcast;
  12. /**创建Activity*/
  13. publicvoidonCreate(BundlesavedInstanceState){
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.broadcast);//使用布局文件
  16. getView();
  17. sendBroadcast.setOnClickListener(sendBroadcastClick);//添加事件监听
  18. }
  19. publicvoidgetView(){
  20. sendBroadcast=(Button)findViewById(R.id.sendBroadcast);
  21. }
  22. /**创建事件监听*/
  23. publicButton.OnClickListenersendBroadcastClick=newButton.OnClickListener(){
  24. publicvoidonClick(Viewview){
  25. Intentintent=newIntent();//创建意图
  26. intent.putExtra("CONTENT","ThisisaBraodcastdemo");//设置广播的内容
  27. intent.setAction("lovefang.stadyService");//设置广播的Action
  28. sendBroadcast(intent);
  29. }
  30. };
  31. }



4.2 处理广播消息

 

[java] view plaincopy
  1. packagelovefang.stadyService;
  2. /***/
  3. importandroid.content.BroadcastReceiver;
  4. importandroid.content.Context;
  5. importandroid.content.Intent;
  6. importandroid.util.Log;
  7. /**这是一个接收广播的类*/
  8. publicclassUseBroadcastReceiverextendsBroadcastReceiver{
  9. publicvoidonReceive(Contextcontext,Intentintent){
  10. Log.v("UseBroadcastReceiver","Igetamessage");
  11. }
  12. }


5.Notification

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

实现的代码如下:

 

[java] view plaincopy
  1. packagelovefang.stadyService;
  2. importandroid.content.Intent;
  3. importandroid.os.Bundle;
  4. importandroid.app.Activity;
  5. importandroid.app.Notification;
  6. importandroid.app.NotificationManager;
  7. importandroid.app.PendingIntent;
  8. importandroid.net.Uri;
  9. importandroid.media.RingtoneManager;
  10. importandroid.widget.Button;
  11. importandroid.view.View;
  12. /**使用notification*/
  13. publicclassUseNotificationextendsActivity{
  14. /**创建组件*/
  15. privateButtontextButton;
  16. privateButtonsoundButton;//声音通知
  17. privateButtonvibrateButton;//震动通知
  18. privateButtonledButton;//led通知
  19. privateButtonoffButton;//关闭通知
  20. NotificationManagernotificationManager;
  21. /**创建Activity*/
  22. publicvoidonCreate(BundlesavedInstanceState){
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.notification);
  25. getComment();
  26. registerComment();
  27. }
  28. /**获取对象*/
  29. publicvoidgetComment(){
  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. publicvoidregisterComment(){
  40. textButton.setOnClickListener(notificationMessage);
  41. soundButton.setOnClickListener(notificationSound);
  42. vibrateButton.setOnClickListener(notificationVibrate);
  43. ledButton.setOnClickListener(notificationLed);
  44. offButton.setOnClickListener(notificationOff);
  45. }
  46. publicButton.OnClickListenernotificationMessage=newButton.OnClickListener(){
  47. publicvoidonClick(Viewview){
  48. Notificationnotification=newNotification();//创建Notification对象
  49. notification.icon=R.drawable.icon;
  50. notification.tickerText="Thisistextnotication";//设置通知消息
  51. /**单击通知后的Intent,此例子单击后还是在当前页面*/
  52. PendingIntentintent=PendingIntent
  53. .getActivity(UseNotification.this,
  54. 0,newIntent(UseNotification.this,UseNotification.class)
  55. ,0);
  56. /**设置通知消息*/
  57. notification.setLatestEventInfo(UseNotification.this
  58. ,"Notification","ContentofNotificationDemo",intent);
  59. /**执行通知*/
  60. notificationManager.notify(0,notification);
  61. }
  62. };
  63. publicButton.OnClickListenernotificationSound=newButton.OnClickListener(){
  64. publicvoidonClick(Viewview){
  65. /**创建通知对象*/
  66. Notificationnotification=newNotification();
  67. /**获取系统当前声音*/
  68. StringringName=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. publicButton.OnClickListenernotificationVibrate=newButton.OnClickListener(){
  79. publicvoidonClick(Viewview){
  80. Notificationnotification=newNotification();//创建Notification对象
  81. notification.vibrate=newlong[]{0,100,200,300};//设置通知震动模式
  82. notificationManager.notify(0,notification);//执行通知
  83. }
  84. };
  85. /**LED通知*/
  86. publicButton.OnClickListenernotificationLed=newButton.OnClickListener(){
  87. publicvoidonClick(Viewview){
  88. Notificationnotification=newNotification();//创建Notification对象
  89. notification.ledOnMS=300;//设置led开始闪光的时间
  90. notification.ledOffMS=1000;//设置关闭时的闪光时间
  91. notificationManager.notify(0,notification);//执行通知
  92. }
  93. };
  94. /**关闭通知*/
  95. publicButton.OnClickListenernotificationOff=newButton.OnClickListener(){
  96. publicvoidonClick(Viewview){
  97. notificationManager.cancel(0);//关闭通知
  98. }
  99. };
  100. }


6.Alarm

闹钟服务

 

[java] view plaincopy
  1. packagelovefang.stadyService;
  2. importandroid.app.Activity;
  3. importandroid.os.Bundle;
  4. importandroid.widget.Button;
  5. importandroid.view.View;
  6. importandroid.app.AlarmManager;
  7. importjava.util.Calendar;
  8. publicclassUseAlarmManagerextendsActivity{
  9. /**创建参数*/
  10. privateButtonstartAlarm;
  11. privateButtonshutdownAlarm;
  12. privateAlarmManageralarm;
  13. /**创建Activity*/
  14. publicvoidonCreate(BundlesavedInstanceState){
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.usealarmmanager);
  17. getWidget();
  18. }
  19. publicvoidgetWidget(){
  20. startAlarm=(Button)findViewById(R.id.startAlarm);
  21. shutdownAlarm=(Button)findViewById(R.id.shutDowntAlarm);
  22. alarm=(AlarmManager)getSystemService(ALARM_SERVICE);//获取AlarmManager
  23. }
  24. publicvoidregisterWidget(){
  25. startAlarm.setOnClickListener(startAlarms);
  26. shutdownAlarm.setOnClickListener(shutdownAlarms);
  27. }
  28. /**启动闹钟*/
  29. publicButton.OnClickListenerstartAlarms=newButton.OnClickListener(){
  30. publicvoidonClick(Viewview){
  31. //设置10秒后出发闹钟
  32. Calendarcalendar=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. publicButton.OnClickListenershutdownAlarms=newButton.OnClickListener(){
  39. publicvoidonClick(Viewview){
  40. alarm.cancel(null);
  41. }
  42. };
  43. }


7.获取手机的状态

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

 

[java] view plaincopy
  1. packagelovefang.stadyService;
  2. /**引入包*/
  3. importandroid.os.Bundle;
  4. importandroid.app.Activity;
  5. importandroid.app.Service;
  6. importandroid.view.View;
  7. importandroid.widget.Button;
  8. importandroid.widget.TextView;
  9. importandroid.content.ContentResolver;//Thisclassprovidesapplicationsaccesstothecontentmodel.
  10. importandroid.telephony.TelephonyManager;
  11. importandroid.util.Log;
  12. /**尖锋网-获取手机的状态*/
  13. publicclassUsePhoneStateextendsActivity{
  14. /**创建参数*/
  15. privateContentResolvercr;
  16. privateButtongetStateButton;//用来获取用户的手机状态
  17. /**创建Activity*/
  18. publicvoidonCreate(BundlesavedInstanceState){
  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. privateButton.OnClickListenergetState=newButton.OnClickListener(){
  30. publicvoidonClick(Viewview){
  31. /**获得TelephonyManager对象*/
  32. TelephonyManagertelephonyManager=(TelephonyManager)getSystemService(Service.TELEPHONY_SERVICE);
  33. /**获取电信网络级别*/
  34. StringteleCode=telephonyManager.getNetworkCountryIso();
  35. /**获取电信网络公司代码*/
  36. StringteleComCode=telephonyManager.getNetworkOperator();
  37. /**获取电信网络公司名称*/
  38. StringteleComName=telephonyManager.getNetworkOperatorName();
  39. /**获取行动通信类型*/
  40. intTypeCode=telephonyManager.getPhoneType();
  41. Stringtype="";
  42. switch(TypeCode){
  43. caseTelephonyManager.PHONE_TYPE_NONE:
  44. type="PHONE_TYPE_NONE";
  45. break;
  46. caseTelephonyManager.PHONE_TYPE_GSM:
  47. type="PHONE_TYPE_GSM";
  48. break;
  49. caseTelephonyManager.PHONE_TYPE_CDMA:
  50. type="PHONE_TYPE_CDMA";
  51. break;
  52. }
  53. /**获取网络类型*/
  54. intnetTypeCode=telephonyManager.getNetworkType();
  55. StringnetType="NETWORK_TYPE_UNKNOW";
  56. switch(netTypeCode){
  57. caseTelephonyManager.NETWORK_TYPE_1xRTT:
  58. netType="NETWORK_TYPE_1xRTT";
  59. break;
  60. caseTelephonyManager.NETWORK_TYPE_CDMA:
  61. netType="NETWORK_TYPE_CDMA";
  62. break;
  63. caseTelephonyManager.NETWORK_TYPE_EDGE:
  64. netType="NETWORK_TYPE_EDGE";
  65. break;
  66. caseTelephonyManager.NETWORK_TYPE_EVDO_0:
  67. netType="NETWORK_TYPE_EVDO_0";
  68. break;
  69. caseTelephonyManager.NETWORK_TYPE_EVDO_A:
  70. netType="NETWORK_TYPE_EVDO_A";
  71. break;
  72. caseTelephonyManager.NETWORK_TYPE_GPRS:
  73. netType="NETWORK_TYPE_GPRS";
  74. break;
  75. caseTelephonyManager.NETWORK_TYPE_HSDPA:
  76. netType="NETWORK_TYPE_HSDPA";
  77. break;
  78. caseTelephonyManager.NETWORK_TYPE_HSPA:
  79. netType="NETWORK_TYPE_HSPA";
  80. break;
  81. caseTelephonyManager.NETWORK_TYPE_HSUPA:
  82. netType="NETWORK_TYPE_HSUPA";
  83. break;
  84. caseTelephonyManager.NETWORK_TYPE_IDEN:
  85. netType="NETWORK_TYPE_IDEN";
  86. break;
  87. caseTelephonyManager.NETWORK_TYPE_UMTS:
  88. netType="NETWORK_TYPE_UMTS";
  89. break;
  90. default:
  91. break;
  92. }
  93. /**获取漫游状态*/
  94. booleanroamStatusCode=telephonyManager.isNetworkRoaming();
  95. StringroamStatus="NOTROAMINF";
  96. if(roamStatusCode){
  97. roamStatus="ROAMING";
  98. }
  99. /**http://www.jfong.cn/ -获取手机唯一标识*/
  100. Stringimei=telephonyManager.getDeviceId();
  101. /**获取手机IMEISV*/
  102. StringimeiSV=telephonyManager.getDeviceSoftwareVersion();
  103. /**获取手机IMSI*/
  104. Stringimsi=telephonyManager.getSubscriberId();
  105. /**http://www.qi788.com/ -蓝牙服务*/
  106. StringstatusCode=android.provider.Settings.System.getString(cr,
  107. android.provider.Settings.System.BLUETOOTH_ON);
  108. StringbulettothStatus="";
  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. StringAirplaneStatus="";
  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. StringdataRoamStatus="";
  127. if(statusCode.equals("1")){
  128. dataRoamStatus="ENABLE";
  129. }else{
  130. dataRoamStatus="DISABLE";
  131. }
  132. TextViewtxt=(TextView)findViewById(R.id.text_showphonestate);
  133. StringBuildersb=newStringBuilder();
  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 plaincopy
  1. packagelovefang.stadyService;
  2. /***/
  3. importandroid.os.Bundle;
  4. importandroid.os.Vibrator;
  5. importandroid.app.Activity;
  6. importandroid.view.View;
  7. importandroid.content.Context;
  8. importandroid.widget.Button;
  9. /**如何实现手机的震动提示Vibrator*/
  10. publicclassUseVibratorextendsActivity{
  11. /***/
  12. privateButtonvibrator_1_Button;
  13. privateButtonvibrator_2_Button;
  14. privateButtonvibrator_3_Button;
  15. privateVibratorvibrator;
  16. /***/
  17. publicvoidonCreate(BundlesavedInstanceState){
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.use_vibrator);
  20. vibrator=(Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
  21. getWidget();
  22. registerWidget();
  23. }
  24. publicvoidgetWidget(){
  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. publicvoidregisterWidget(){
  30. vibrator_1_Button.setOnClickListener(vibrator_1);
  31. vibrator_2_Button.setOnClickListener(vibrator_2);
  32. vibrator_3_Button.setOnClickListener(vibrator_3);
  33. }
  34. /**震动一次*/
  35. publicButton.OnClickListenervibrator_1=newButton.OnClickListener(){
  36. publicvoidonClick(Viewview){
  37. /**long参数数组里大参数的含义*/
  38. /**第一个参数表示等待100毫秒后开始震动*/
  39. /**第二个参数表示震动100毫秒后停止震动*/
  40. vibrator.vibrate(newlong[]{100,100},0);
  41. }
  42. };
  43. /**震动两次*/
  44. publicButton.OnClickListenervibrator_2=newButton.OnClickListener(){
  45. publicvoidonClick(Viewview){
  46. vibrator.vibrate(newlong[]{1000,3000,1000,3000},0);
  47. }
  48. };
  49. /**震动三次*/
  50. publicButton.OnClickListenervibrator_3=newButton.OnClickListener(){
  51. publicvoidonClick(Viewview){
  52. vibrator.vibrate(newlong[]{1000,1000,1000,2000,1000,300},0);
  53. }
  54. };
  55. }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics