一个Demo学完Android中所有的服务

注:这个例子来自“安卓巴士”,经阅读,理解,写此文章


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


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


1.MainActivity--主界面

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

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

2.启动服务按钮

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

代码如下:

  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.           
  14.         public IBinder onBind(Intent intent){  
  15.             return null;  
  16.         }  
  17.         public void onCreate(){  
  18.             super.onCreate();  
  19.                 /**创建一个线程,每秒计数器加一,并在控制台进行Log输出*/  
  20.             new Thread(new Runnable(){  
  21.                 public void run(){  
  22.                     while(!threadDisable){  
  23.                         try{  
  24.                             Thread.sleep(1000);  
  25.                         }catch(InterruptedException e){  
  26.                               
  27.                         }  
  28.                         count++;  
  29.                         Log.v("CountService","Count is"+count);  
  30.                     }  
  31.                 }  
  32.             }).start();  
  33.         }  
  34.         public void onDestroy(){  
  35.             super.onDestroy();  
  36.                 /**服务停止时,终止计数进程*/  
  37.             this.threadDisable = true;  
  38.         }  
  39.         public int getConunt(){  
  40.             return count;  
  41.         }  
  42.         class ServiceBinder extends Binder{  
  43.             public CountService getService(){  
  44.                 return CountService.this;  
  45.             }  
  46.         }  
  47.     }  

3.绑定服务

服务有两种实现的方法:

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

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

下面是实现的代码:

  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.   
  12. /**通过bindService和unBindSerivce的方式启动和结束服务*/  
  13.     public class UseBrider extends Activity {  
  14.             /**参数设置*/  
  15.         CountService countService;  
  16.       
  17.         @Override  
  18.         public void onCreate(Bundle savedInstanceState) {  
  19.             super.onCreate(savedInstanceState);  
  20.             setContentView(new UseBriderFace(this));  
  21.             Intent intent = new Intent(UseBrider.this,CountService.class);  
  22.                 /**进入Activity开始服务*/  
  23.             bindService(intent, conn, Context.BIND_AUTO_CREATE);  
  24.               
  25.         }  
  26.         private ServiceConnection conn = new ServiceConnection(){  
  27.                 /**获取服务对象时的操作*/   
  28.             public void onServiceConnected(ComponentName name, IBinder service) {  
  29.                 // TODO Auto-generated method stub  
  30.                 countService = ((CountService.ServiceBinder)service).getService();  
  31.                   
  32.             }  
  33.                 /**无法获取到服务对象时的操作*/  
  34.             public void onServiceDisconnected(ComponentName name) {  
  35.                 // TODO Auto-generated method stub  
  36.                 countService =null;  
  37.             }  
  38.               
  39.               
  40.         };  
  41.         protected void onDestroy(){  
  42.             super.onDestroy();  
  43.             this.unbindService(conn);  
  44.             Log.v("MainStadyServics""out");  
  45.         }  
  46.     }  

4.发送广播

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

实现的代码如下:

4.1 打开广播服务

  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.           
  32.     }  


4.2 处理广播消息

  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

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

实现的代码如下:

  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.                             0new 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[] {0100200300};// 设置通知震动模式  
  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

闹钟服务

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

7.获取手机的状态

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

  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.               
  22.             cr = getContentResolver();  
  23.             Log.v("UsePhonestate","cr = getContentResolver()");  
  24.             Log.v("UsePhonestate","setContentView");  
  25.             getStateButton = (Button) findViewById(R.id.button_getphonestate);  
  26.             Log.v("UsePhonestate","getStateButton");  
  27.             getStateButton.setOnClickListener(getState);  
  28.             Log.v("UsePhonestate","getStateButton.setOnClickListener");  
  29.         }  
  30.         private Button.OnClickListener getState = new Button.OnClickListener(){  
  31.             public void onClick(View view){  
  32.                     /**获得TelephonyManager对象*/  
  33.                 TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Service.TELEPHONY_SERVICE);  
  34.                     /**获取电信网络级别*/  
  35.                 String teleCode = telephonyManager.getNetworkCountryIso();  
  36.                     /**获取电信网络公司代码*/  
  37.                 String teleComCode = telephonyManager.getNetworkOperator();  
  38.                     /**获取电信网络公司名称*/  
  39.                 String teleComName = telephonyManager.getNetworkOperatorName();  
  40.                     /**获取行动通信类型*/  
  41.                 int TypeCode = telephonyManager.getPhoneType();  
  42.                   
  43.                 String type = "";  
  44.                   
  45.                 switch(TypeCode){  
  46.                     case TelephonyManager.PHONE_TYPE_NONE:  
  47.                         type = "PHONE_TYPE_NONE";  
  48.                         break;  
  49.                     case TelephonyManager.PHONE_TYPE_GSM:  
  50.                         type = "PHONE_TYPE_GSM";  
  51.                         break;  
  52.                     case TelephonyManager.PHONE_TYPE_CDMA:  
  53.                         type = "PHONE_TYPE_CDMA";  
  54.                         break;  
  55.                 }  
  56.                     /**获取网络类型*/  
  57.                 int netTypeCode = telephonyManager.getNetworkType();  
  58.                 String netType = "NETWORK_TYPE_UNKNOW";  
  59.                 switch(netTypeCode){  
  60.                     case TelephonyManager.NETWORK_TYPE_1xRTT:  
  61.                         netType = "NETWORK_TYPE_1xRTT";  
  62.                         break;  
  63.                     case TelephonyManager.NETWORK_TYPE_CDMA:  
  64.                         netType = "NETWORK_TYPE_CDMA";  
  65.                         break;  
  66.                     case TelephonyManager.NETWORK_TYPE_EDGE:  
  67.                         netType = "NETWORK_TYPE_EDGE";  
  68.                         break;  
  69.                     case TelephonyManager.NETWORK_TYPE_EVDO_0:  
  70.                         netType = "NETWORK_TYPE_EVDO_0";  
  71.                         break;  
  72.                     case TelephonyManager.NETWORK_TYPE_EVDO_A:  
  73.                         netType = "NETWORK_TYPE_EVDO_A";  
  74.                         break;  
  75.                     case TelephonyManager.NETWORK_TYPE_GPRS:  
  76.                         netType = "NETWORK_TYPE_GPRS";  
  77.                         break;  
  78.                     case TelephonyManager.NETWORK_TYPE_HSDPA:  
  79.                         netType = "NETWORK_TYPE_HSDPA";  
  80.                         break;  
  81.                     case TelephonyManager.NETWORK_TYPE_HSPA:  
  82.                         netType = "NETWORK_TYPE_HSPA";  
  83.                         break;  
  84.                     case TelephonyManager.NETWORK_TYPE_HSUPA:  
  85.                         netType = "NETWORK_TYPE_HSUPA";  
  86.                         break;  
  87.                     case TelephonyManager.NETWORK_TYPE_IDEN:  
  88.                         netType = "NETWORK_TYPE_IDEN";  
  89.                         break;  
  90.                     case TelephonyManager.NETWORK_TYPE_UMTS:  
  91.                         netType = "NETWORK_TYPE_UMTS";  
  92.                         break;  
  93.                     default:  
  94.                         break;  
  95.                 }  
  96.                   
  97.                     /**获取漫游状态*/  
  98.                 boolean roamStatusCode = telephonyManager.isNetworkRoaming();  
  99.                 String roamStatus = "NOT ROAMINF";  
  100.                 if(roamStatusCode){  
  101.                     roamStatus = "ROAMING";  
  102.                 }  
  103.                   
  104.                     /**获取手机唯一标识*/  
  105.                 String imei = telephonyManager.getDeviceId();  
  106.                     /**获取手机IMEI SV*/  
  107.                 String imeiSV = telephonyManager.getDeviceSoftwareVersion();  
  108.                     /**获取手机IMSI*/  
  109.                 String imsi = telephonyManager.getSubscriberId();  
  110.                   
  111.                     /**蓝牙服务*/  
  112.                 String statusCode = android.provider.Settings.System.getString(cr,  
  113.                         android.provider.Settings.System.BLUETOOTH_ON);  
  114.                 String bulettothStatus = "";  
  115.                 if(statusCode.equals("1")){  
  116.                     bulettothStatus = "ENABLE";  
  117.                 }else{  
  118.                     bulettothStatus = "DISABLE";  
  119.                 }  
  120.                   
  121.                     /**飞行模式是否打开*/  
  122.                 statusCode = android.provider.Settings.System.getString(cr,  
  123.                         android.provider.Settings.System.AIRPLANE_MODE_ON);  
  124.                   
  125.                 String AirplaneStatus = "";  
  126.                 if(statusCode.equals("1")){  
  127.                     AirplaneStatus = "ENABLE";  
  128.                 }else{  
  129.                     AirplaneStatus = "DISABLE";  
  130.                 }  
  131.                   
  132.                     /**数据漫游模式是否打开*/  
  133.                 statusCode = android.provider.Settings.System.getString(cr,  
  134.                         android.provider.Settings.System.DATA_ROAMING);  
  135.                 String dataRoamStatus = "";  
  136.                 if(statusCode.equals("1")){  
  137.                     dataRoamStatus = "ENABLE";  
  138.                 }else{  
  139.                     dataRoamStatus = "DISABLE";  
  140.                 }  
  141.                 TextView txt = (TextView) findViewById(R.id.text_showphonestate);  
  142.                 StringBuilder sb = new StringBuilder();  
  143.                 sb.append("teleCode: "+teleCode+"\n");  
  144.                 sb.append("teleComCode: "+teleComCode+"\n");  
  145.                 sb.append("teleComName: "+teleComName+"\n");  
  146.                 sb.append("type: "+type+"\n");  
  147.                 sb.append("netType: "+netType+"\n");  
  148.                 sb.append("roamStatus: "+roamStatus+"\n");  
  149.                 sb.append("imei: "+imei+"\n");  
  150.                 sb.append("imeiSV: "+imeiSV+"\n");  
  151.                 sb.append("imsi: "+imsi+"\n");  
  152.                 sb.append("bulettothStatus: "+bulettothStatus+"\n");  
  153.                 sb.append("AirplaneStatus: "+AirplaneStatus+"\n");  
  154.                 sb.append("dataRoamStatus: "+dataRoamStatus+"\n");  
  155.                   
  156.                 txt.setText(sb.toString());  
  157.             }  
  158.         };  
  159.     }  

8.Vibrator

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

  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.           
  25.         public void getWidget(){  
  26.             vibrator_1_Button = (Button) findViewById(R.id.button_vibrator_1);  
  27.             vibrator_2_Button = (Button) findViewById(R.id.button_vibrator_2);  
  28.             vibrator_3_Button = (Button) findViewById(R.id.button_vibrator_3);  
  29.         }  
  30.           
  31.         public void registerWidget(){  
  32.             vibrator_1_Button.setOnClickListener(vibrator_1);  
  33.             vibrator_2_Button.setOnClickListener(vibrator_2);  
  34.             vibrator_3_Button.setOnClickListener(vibrator_3);  
  35.         }  
  36.             /**震动一次*/  
  37.         public Button.OnClickListener vibrator_1 = new Button.OnClickListener(){  
  38.             public void onClick(View view){  
  39.                     /**long参数数组里大参数的含义*/  
  40.                     /**第一个参数表示等待100毫秒后开始震动*/  
  41.                     /**第二个参数表示震动100毫秒后停止震动*/  
  42.                 vibrator.vibrate(new long[]{100,100}, 0);  
  43.             }  
  44.         };  
  45.             /**震动两次*/  
  46.         public Button.OnClickListener vibrator_2 = new Button.OnClickListener(){  
  47.             public void onClick(View view){  
  48.                 vibrator.vibrate(new long[]{1000,3000,1000,3000}, 0);  
  49.             }  
  50.         };  
  51.             /**震动三次*/  
  52.         public Button.OnClickListener vibrator_3 = new Button.OnClickListener(){  
  53.             public void onClick(View view){  
  54.                 vibrator.vibrate(new long[]{1000,1000,1000,2000,1000,300}, 0);  
  55.             }  
  56.         };  
  57.     }  



下面给出源代码的下载地址:

http://download.csdn.net/detail/dlutbrucezhang/5061544
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值