2010年4月26日 星期一

Android學習筆記 - 背景執行服務(Service)

1. 大部分使用者在手機上看到的畫面都是前端的程式(Activity),但是還是要有很多的服務需在背景執行。
如果是要在背景執行的程式,則需要寫成Service並繼承android.app.Service,由於是在背景執行所以是
要寫成Service而不是Activity,因此需在AndroidManifest新增一個Service。以下範例是按下Start Service之後會
在背景每隔一秒Log目前的時間,按下Stop Service後會停止Log的動作。



2. 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>



6. 執行之後的畫面。


3 則留言:

  1. @@a 你沒有在 AndroidManifest 中加入 Service 喔....

    回覆刪除
  2. 請問為何 start 和 stop service 用的是不同的 intent ..
    這樣 stopService 能正確 destroy 到 service 嗎?

    回覆刪除
  3. 您好..我是一個android的程式設計初學者,想請問一下..您是否會寫錄影程式??

    回覆刪除