大部分使用者在手機上看到的畫面都是前端的程式(Activity),但是還是要有很多的服務需在背景執行。
如果是要在背景執行的程式,則需要寫成Service並繼承android.app.Service,由於是在背景執行所以是
要寫成Service而不是Activity,因此需在AndroidManifest新增一個Service。以下範例是按下Start Service之後會在背景每隔一秒Log目前的時間,按下Stop Service後會停止Log的動作。
====================================================
MainActivity.java
====================================================
package org.me.android_service; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button startButton; private Button stopButton; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); startButton = (Button) findViewById(R.id.startButton); stopButton = (Button) findViewById(R.id.stopButton); startButton.setOnClickListener(startClickListener); stopButton.setOnClickListener(stopClickListener); } private Button.OnClickListener startClickListener = new Button.OnClickListener() { public void onClick(View arg0) { //啟動服務 Intent intent = new Intent(MainActivity.this, NickyService.class); startService(intent); } }; private Button.OnClickListener stopClickListener = new Button.OnClickListener() { public void onClick(View arg0) { //停止服務 Intent intent = new Intent(MainActivity.this, NickyService.class); stopService(intent); } }; }
3. NickyService.java
package org.me.android_service; import android.app.Service; import android.content.Intent; import android.os.Handler; import android.os.IBinder; import android.util.Log; import java.util.Date; //繼承android.app.Service public class NickyService extends Service { private Handler handler = new Handler(); @Override public IBinder onBind(Intent intent) { return null; } @Override public void onStart(Intent intent, int startId) { handler.postDelayed(showTime, 1000); super.onStart(intent, startId); } @Override public void onDestroy() { handler.removeCallbacks(showTime); super.onDestroy(); } private Runnable showTime = new Runnable() { public void run() { //log目前時間 Log.i("time:", new Date().toString()); handler.postDelayed(this, 1000); } }; }
4. main.xml(Layout)
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:id="@+id/widget0" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" > <Button android:id="@+id/startButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Start Service" android:layout_x="40px" android:layout_y="67px" > </Button> <Button android:id="@+id/stopButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Stop Service" android:layout_x="140px" android:layout_y="67px" > </Button> </AbsoluteLayout>
5. AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.me.android_spinner"> <application> <activity android:name=".MainActivity" android:label="MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> <service android:name=".NickyService"/> </application> </manifest>
沒有留言:
張貼留言