Androidのすべてのサービスを学び終えたDemo

382009 ワード

この例は「アンドロイドバス」から来て、読んで、理解して、この文章を書いて、この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秒おきに1行のログを印刷します.
コードは次のとおりです.

  
  
  
  
  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.バインドサービス
サービスには2つの方法があります.
(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.     }   

次に、ブロードキャスト、Notification通知、Alarm目覚まし時計などのサービスの具体的な内容をご紹介します
4.放送の送信
sendBroadcastを使用して、1つのActionにブロードキャストを送信し、対応するブロードキャスト受信機によって対応する動作を受信して実行する
実装されるコードは次のとおりです.
(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.     }   

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

次のページでは、携帯電話の状態や、Vibratorの振動機能などのサービスの詳細をご紹介します
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+"
    "
    );   
  144.                 sb.append("teleComCode: "+teleComCode+"
    "
    );   
  145.                 sb.append("teleComName: "+teleComName+"
    "
    );   
  146.                 sb.append("type: "+type+"
    "
    );   
  147.                 sb.append("netType: "+netType+"
    "
    );   
  148.                 sb.append("roamStatus: "+roamStatus+"
    "
    );   
  149.                 sb.append("imei: "+imei+"
    "
    );   
  150.                 sb.append("imeiSV: "+imeiSV+"
    "
    );   
  151.                 sb.append("imsi: "+imsi+"
    "
    );   
  152.                 sb.append("bulettothStatus: "+bulettothStatus+"
    "
    );   
  153.                 sb.append("AirplaneStatus: "+AirplaneStatus+"
    "
    );   
  154.                 sb.append("dataRoamStatus: "+dataRoamStatus+"
    "
    );   
  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.     }  

次に、ソースコードのダウンロードアドレスを示します(注:サードパーティのWebサイトのダウンロードリソースは登録する必要があります):
http://download.csdn.net/detail/dlutbrucezhang/5061544
このDemoを通じてAndroidのすべてのサービスを学び、多くの読者に役立つことを望んでいます.テキストアドレスhttp://mobile.51cto.com/android-386769_2.htm