開啟服務印出會累加數字的Log
※因Service是背景服務,因此利用印Log來驗證
1). 設置一個Activity放置啟動/關閉Service的button - main.xml, ServiceTest1.java
2). 設置一隻繼承Service類別的java檔 - mService.java
3). Service包含onCreate, onStart(可不用), onDestroy,另外需複寫public IBinder onBind
4). 程式設計:onCreate宣告服務開始,並每隔1秒呼叫Runnable物件tasks
Runnable中每隔一秒呼叫postDelayed的method,反覆進行
onDestroy宣告當服務結束,移除tasks執行緒
5). 在AndroidManifest.xml宣告service(mService.xml)
//=============================================================
[strings.xml]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Please press the button to start or stop service, and notice the logcat message.</string>
<string name="app_name">ServiceTest1</string>
<string name="button_start">Start Service</string>
<string name="button_stop">Stop Service</string>
</resources>
//-------------------------------------------------------------------------------------------------------------
[main.xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_start" />
<Button
android:id="@+id/button_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_stop" />
</LinearLayout>
//-------------------------------------------------------------------------------------------------------------
[ServiceTest1.java]
package com.servicetest1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ServiceTest1 extends Activity {
private Button button_start;
private Button button_stop;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 取得資源類別的介面元件
button_start = (Button) findViewById(R.id.button_start);
button_stop = (Button) findViewById(R.id.button_stop);
// 設定button的監聽並執行的method
button_start.setOnClickListener(startClickListener);
button_stop.setOnClickListener(stopClickListener);
}
// startService的method
private Button.OnClickListener startClickListener = new Button.OnClickListener() {
@Override
public void onClick(View v) {
// 宣告intent,並指定要啟動的class
Intent intent = new Intent(ServiceTest1.this, mService.class);
// 以startservice方式啟動intent
startService(intent);
}
};
// stopService的method
private Button.OnClickListener stopClickListener = new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(ServiceTest1.this, mService.class);
stopService(intent);
}
};
}
//-------------------------------------------------------------------------------------------------------------
[mService.java]
package com.servicetest1;
import java.util.Date;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class mService extends Service {
// 建立Handler物件,作為執行緒傳遞postDelayed
private Handler handle = new Handler();
// 確認Service狀況
private int count = 0;
@Override
public void onCreate() {
// 服務開始,每隔1秒呼叫tasks
handle.postDelayed(tasks, 1000);
Log.d("create", "Count: " + Integer.toString(count));
super.onCreate();
}
private Runnable tasks = new Runnable() {
@Override
public void run() {
count++;
// 每隔一秒呼叫task執行緒
handle.postDelayed(tasks, 1000);
Log.d("run", "RunCount: " + Integer.toString(count));
// 印現在時間
Log.d("time", "Time: " + new Date().toString());
}
};
@Override
public IBinder onBind(Intent arg0) {
// IBinder方法為service建構必須副寫的方法
return null;
}
@Override
public void onDestroy() {
// 當服務結束移除task
handle.removeCallbacks(tasks);
Log.d("onDestroy","End");
super.onDestroy();
}
}
//-------------------------------------------------------------------------------------------------------------
[AndroidManifest.xml]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.servicetest1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ServiceTest1"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".mService"
android:exported="true"
android:process=":remote" >
</service>
</application>
</manifest>
沒有留言:
張貼留言