這是一個幫助理解活動(Activity),服務(Service)和廣播接收器(BroadcastReceiver)很好的練習. 如果能消化這段代碼, 絕對可以增加對活動,服務和廣播接收器的了解.
本應用程序包括一個活動(testActivity)和一個服務(testService), 他們各自有自己的廣播接收器(MyReceiver 和 TestServiceReceiver).
當按下Start按鈕, testActivity會通過意圖(Intent)啟動testService.
在testService的onStartCommand()創建並啟動一個線程(thread), 在線程裡面重複發送廣播(sendBroadcast()), 把當前系統時間送給testActivity 的MyReceiver.
當MyReceiver接收到, 會把接收到的時間和新的系統時間比較, 並把延遲時間顯示出來.
當按下Stop按鈕, testActivity會發送廣播送給testService 的 TestServiceReceiver.
當TestServiceReceiver接收到, 便會停止服務.
testActivity.java
package com.testActivity;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class testActivity extends Activity {
final static String MY_ACTION = "testActivity.MY_ACTION";
TextView textData;
public static final int RQS_STOP_SERVICE = 1;
MyReceiver myReceiver;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textData = (TextView)findViewById(R.id.data);
Button buttonStart = (Button)findViewById(R.id.start);
Button buttonStop = (Button)findViewById(R.id.stop);
buttonStart.setOnClickListener(buttonStartOnClickListener);
buttonStop.setOnClickListener(buttonStopOnClickListener);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
myReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MY_ACTION);
registerReceiver(myReceiver, intentFilter);
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
unregisterReceiver(myReceiver);
super.onStop();
}
Button.OnClickListener buttonStartOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(testActivity.this, com.testActivity.testService.class);
testActivity.this.startService(intent);
}};
Button.OnClickListener buttonStopOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction(testService.MY_ACTION);
intent.putExtra("RQS", RQS_STOP_SERVICE);
sendBroadcast(intent);
}};
private class MyReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
long timestamp = arg1.getLongExtra("timestamp", 0);
long curtime = System.currentTimeMillis();
long delay = curtime - timestamp;
textData.setText(String.valueOf(timestamp)
+ " : " + String.valueOf(curtime)
+ " delay " + String.valueOf(delay)
+ "(ms)");
}
}
}
testService.java
package com.testActivity;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.widget.Toast;
public class testService extends Service {
final static String MY_ACTION = "testService.MY_ACTION";
TestServiceReceiver testServiceReceiver;
boolean running;
@Override
public void onCreate() {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),
"TestServiceReceiver.onCreate",
Toast.LENGTH_LONG).show();
testServiceReceiver = new TestServiceReceiver();
super.onCreate();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),
"TestServiceReceiver.onStartCommand",
Toast.LENGTH_LONG).show();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(MY_ACTION);
registerReceiver(testServiceReceiver, intentFilter);
running = true;
MyThread myThread = new MyThread();
myThread.start();
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
this.unregisterReceiver(testServiceReceiver);
super.onDestroy();
}
public class MyThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
while(running){
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Intent intent = new Intent();
intent.setAction(testActivity.MY_ACTION);
intent.putExtra("timestamp", System.currentTimeMillis());
sendBroadcast(intent);
}
}
}
public class TestServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
int rqs = arg1.getIntExtra("RQS", 0);
if (rqs == testActivity.RQS_STOP_SERVICE){
Toast.makeText(getBaseContext(),
"TestServiceReceiver.onReceive w/ RQS_STOP_SERVICE",
Toast.LENGTH_LONG).show();
running = false;
stopSelf();
}
}
}
}
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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Start -"
/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="- Stop -"
/>
<TextView
android:id="@+id/data"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
修改AndroidManifest.xml添加 testService
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testActivity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".testActivity"
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=".testService"
/>
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>