? Activity 好像是應用程式的眼睛,提供與User 互動之窗。
? BroadcastReceiver 好像是耳朵,接收來自各方的Intent。
? Service 好像是手,提供符合Intent 意圖之服務。
10.2.1 操作情境:
1. 此程式一開始,畫面出現兩個按鈕如下:
2. 按下<call_service>按鈕,暫停15 秒:
3. 等待15 秒後,委託Alarm Manager 發出intent。當BroadcastReceiver 接到intent時,就啟動NotifyService,此服務會回傳字串,顯示於ac01 畫面的Title 區域:
4. 按下<Exit>,程式就結束了。
10.2.2 撰寫步驟:
Step-1: 建立Android 專案:kx02。
Step-2: 撰寫BroadcastReceiver 的子類別:AlarmReceiver,其程式碼如下:、
package com.misoo.kx02; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class AlarmReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { context.startService(new Intent(context, NotifyService.class)); }}
Step-3: 撰寫Service 的子類別:NotifyService,其程式碼如下:
package com.misoo.kx02; import android.app.Service; import android.content.Intent; import android.os.IBinder; public class NotifyService extends Service{ @Override protected void onCreate() { ac01 app = ac01.getApp(); app.btEvent("from NotifyService"); } @Override public IBinder onBind(Intent intent) { return null; } }
Step-4: 撰寫Activity 的子類別:ac01,其程式碼如下:
package com.misoo.kx02; import java.util.Calendar; import android.app.Activity; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.widget.Button; import android.view.View; import android.view.View.OnClickListener; public class ac01 extends Activity implements OnClickListener{ private static ac01 appRef = null; private Button btn, btn2; boolean k = false; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); appRef = this; setContentView(R.layout.cs); btn = (Button)findViewById(R.id.call_service); btn.setOnClickListener(this); btn2 = (Button)findViewById(R.id.exit); btn2.setOnClickListener(this); } public static ac01 getApp() { return appRef; } public void btEvent( String data ) { setTitle(data); } public void onClick(View arg0) { if(arg0 == btn){ setTitle("Waiting... Alarm=15"); Intent intent = new Intent(ac01.this, AlarmReceiver.class); PendingIntent p_intent = PendingIntent.getBroadcast(ac01.this, 0, intent, 0); Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.add(Calendar.SECOND, 15); // Schedule the alarm! AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), p_intent); } if(arg0 == btn2){ Intent intent = new Intent(ac01.this, AlarmReceiver.class); PendingIntent p_intent = PendingIntent.getBroadcast(ac01.this, 0, intent, 0); AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); am.cancel(p_intent); finish(); }}}
Step-5: 修改AndroidManifest.xml 的內容,更改為:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.misoo.kx02"> <application android:icon="@drawable/icon"> <activity android:name=".ac01" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name=".AlarmReceiver"> </receiver> <service android:name=".NotifyService"> </service> </application> </manifest>
由Activity发送过来一个消息,然后BroadcastReceiver收到,再发送intent去启动Service。
onBind负责intent的接收。Service的任务就是调用btEvent这个方法。getApp是一个Activity的方法主要是负责得到那个Activity的Context。
Activity向BroadcastReceiver发送消息,然后Receiver收到消息之后发送Intent去Service,Service就会根据消息作出一些影响UI的行动。Service实际就是一个后台运行的Activity。