Android 中的服務 (Service) 範例
Android簡介使用方法開發工具開始寫程式視覺化介面視覺元件對話元件核心物件事件處理資料儲存查詢功能影音功能繪圖功能網路功能衛星地圖特殊功能資源管理裝置管理系統核心問題與回答刷機升級常用軟體Eclipse教學錄影訊息相關網站參考文獻最新修改簡體版English |
專案下載:MediaService1.zip 執行畫面程式:MediaController.javapackage ccc.android; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MediaController extends Activity implements OnClickListener { private static final String TAG = "MediaControl"; Button buttonStart, buttonStop; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonStart = (Button) findViewById(R.id.Play); buttonStop = (Button) findViewById(R.id.Stop); buttonStart.setOnClickListener(this); buttonStop.setOnClickListener(this); } public void onClick(View src) { switch (src.getId()) { case R.id.Play: Log.d(TAG, "onClick: starting srvice"); startService(new Intent(this, MusicService.class)); // startService(new Intent("ccc.android.MUSIC")); break; case R.id.Stop: Log.d(TAG, "onClick: stopping srvice"); stopService(new Intent(this, MusicService.class)); // stopService(new Intent("ccc.android.MUSIC")); break; } } } 程式:MusicService.javapackage ccc.android; import android.app.Service; import android.content.Intent; import android.media.MediaPlayer; import android.os.IBinder; public class MusicService extends Service { private static final String TAG = "MusicService"; MediaPlayer player; @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } @Override public void onCreate() { player = MediaPlayer.create(this, R.raw.test); player.setLooping(false); // Set looping } @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); player.start(); } @Override public void onDestroy() { super.onDestroy(); player.stop(); } } 檔案:res/layout/main.xml<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/Play" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="開始播放"/> <Button android:id="@+id/Stop" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="停止播放"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> 檔案:res/AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ccc.android" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MediaController" android:label="@string/app_name" android:debuggable="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:enabled="true" android:name=".MusicService"> <intent-filter> <action android:name="ccc.android.MUSIC"/> <category android:name="android.intent.category.default"/> </intent-filter> </service> </application> <uses-sdk android:minSdkVersion="4" /> </manifest> 參考文獻
|
page revision: 5, last edited: 23 Feb 2012 03:15
Post preview:
Close preview