這個例子來自「安卓巴士」,經閱讀,理解,寫此文章,但願經過這篇一個Demo學完Android中全部的服務對對廣大讀者有所幫助。
android
說明:這個例子實現了Android中常見的許多服務,下面是實現的截圖app
接下來,以源代碼的方式分析這個例子
異步
1.MainActivity--主界面async
這個類主要是實現用戶所看到的這個Activity,其中包含了一系列的按鈕,用戶點擊按鈕執行相應的動做,因此在這個類中主要是對按鈕的定義和對按鈕綁定相應的監聽器,下面是實現的代碼:ide
package lovefang.stadyService; import android.app.Activity; import android.os.Bundle; import android.widget.Button; import android.view.View; import android.content.Intent; import android.util.Log; /**這是使用後臺服務的學習例子*/ public class MainStadyServics extends Activity { /**參數設置*/ Button startServiceButton;// 啓動服務按鈕 Button shutDownServiceButton;// 關閉服務按鈕 Button startBindServiceButton;// 啓動綁定服務按鈕 Button sendBroadcast;// 使用廣播 Button notificationButton;// 使用通知功能 Button alarmButton;// 使用鬧鐘 Button handlerButton;// 使用handler Button asyncButton;// 使用異步加載 Button phoneStateButton;// 查看手機狀態 Button callphoneButton;// 撥打電話 Button vibratorButton;// 使用震動 CountService countService; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v("MainStadyServics", "setContentView"); setContentView(R.layout.main); getWidget(); regiestListener(); } /**得到組件*/ public void getWidget(){ startServiceButton = (Button)findViewById(R.id.startServerButton); startBindServiceButton = (Button)findViewById(R.id.startBindServerButton); shutDownServiceButton = (Button)findViewById(R.id.sutdownServerButton); sendBroadcast = (Button)findViewById(R.id.sendBroadcast); notificationButton = (Button)findViewById(R.id.notification); alarmButton = (Button)findViewById(R.id.alarm); handlerButton = (Button)findViewById(R.id.handler); asyncButton = (Button)findViewById(R.id.async); phoneStateButton = (Button) findViewById(R.id.phonestate); callphoneButton = (Button) findViewById(R.id.callphone); vibratorButton = (Button) findViewById(R.id.vibrator); } /**爲按鈕添加監聽*/ public void regiestListener(){ startServiceButton.setOnClickListener(startService); shutDownServiceButton.setOnClickListener(shutdownService); startBindServiceButton.setOnClickListener(startBinderService); sendBroadcast.setOnClickListener(broadcastReceiver); notificationButton.setOnClickListener(notification); alarmButton.setOnClickListener(startAlarm); handlerButton.setOnClickListener(handler); asyncButton.setOnClickListener(async); phoneStateButton.setOnClickListener(phonestate); callphoneButton.setOnClickListener(callphoneEvent); vibratorButton.setOnClickListener(vibrator); } /**啓動服務的事件監聽*/ public Button.OnClickListener startService = new Button.OnClickListener(){ public void onClick(View view){ /**單擊按鈕時啓動服務*/ Intent intent = new Intent(MainStadyServics.this,CountService.class); startService(intent); Log.v("MainStadyServics", "start Service"); } }; /**關閉服務*/ public Button.OnClickListener shutdownService = new Button.OnClickListener(){ public void onClick(View view){ /**單擊按鈕時啓動服務*/ Intent intent = new Intent(MainStadyServics.this,CountService.class); /**退出Activity是,中止服務*/ stopService(intent); Log.v("MainStadyServics", "shutDown serveice"); } }; /**打開綁定服務的Activity*/ public Button.OnClickListener startBinderService = new Button.OnClickListener(){ public void onClick(View view){ /**單擊按鈕時啓動服務*/ Intent intent = new Intent(MainStadyServics.this,UseBrider.class); startActivity(intent); Log.v("MainStadyServics", "start Binder Service"); } }; /**打開廣播學習的按鈕*/ public Button.OnClickListener broadcastReceiver = new Button.OnClickListener(){ public void onClick(View view){ Intent intent = new Intent(MainStadyServics.this,UseBroadcast.class); startActivity(intent); Log.v("MainStadyServics","start broadcast"); } }; /**打開通知*/ public Button.OnClickListener notification = new Button.OnClickListener(){ public void onClick(View view){ Intent intent = new Intent(MainStadyServics.this, UseNotification.class); startActivity(intent); Log.v("MainStadyService ","start Notification"); } }; /**使用鬧鐘*/ public Button.OnClickListener startAlarm = new Button.OnClickListener(){ public void onClick(View view){ Intent intent = new Intent(MainStadyServics.this, UseAlarmManager.class); startActivity(intent); Log.v("MainStadyService ","start alarm"); } }; public Button.OnClickListener handler= new Button.OnClickListener(){ public void onClick(View view){ Intent intent = new Intent(MainStadyServics.this, UseHandleMessage.class); startActivity(intent); Log.v("MainStadyService ","start handle"); } }; public Button.OnClickListener async= new Button.OnClickListener(){ public void onClick(View view){ Intent intent = new Intent(MainStadyServics.this, UseAsyncTask.class); startActivity(intent); Log.v("MainStadyService ","start handle"); } }; public Button.OnClickListener phonestate= new Button.OnClickListener(){ public void onClick(View view){ Intent intent = new Intent(MainStadyServics.this, UsePhoneState.class); startActivity(intent); Log.v("MainStadyService ","start phonestate"); } }; public Button.OnClickListener callphoneEvent= new Button.OnClickListener(){ public void onClick(View view){ Intent intent = new Intent(MainStadyServics.this, UseActionCall.class); startActivity(intent); Log.v("MainStadyService ","start callphone"); } }; public Button.OnClickListener vibrator= new Button.OnClickListener(){ public void onClick(View view){ Intent intent = new Intent(MainStadyServics.this, UseVibrator.class); startActivity(intent); Log.v("MainStadyService ","start callphone"); } }; /***/ protected void onDestroy(){ super.onDestroy(); Intent intent = new Intent(MainStadyServics.this,CountService.class); /**退出Activity時,中止服務*/ stopService(intent); } }