Activity、BroadcastReceiver、Service共同工作的简单例子
? Activity 好像是應用程式的眼睛,提供與User 互動之窗。
? BroadcastReceiver 好像是耳朵,接收來自各方的Intent。
? Service 好像是手,提供符合Intent 意圖之服務。
10.2.1 操作情境:
1. 此程式一開始,畫面出現(xiàn)兩個按鈕如下:
2. 按下<call_service>按鈕,暫停15 秒:
3. 等待15 秒後,委託Alarm Manager 發(fā)出intent。當BroadcastReceiver 接到intent時,就啟動NotifyService,此服務會回傳字串,顯示於ac01 畫面的Title 區(qū)域:
4. 按下<Exit>,程式就結(jié)束了。
10.2.2 撰寫步驟:
Step-1: 建立Android 專案:kx02。
Step-2: 撰寫B(tài)roadcastReceiver 的子類別:AlarmReceiver,其程式碼如下:、
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 的內(nèi)容,更改為:
<?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發(fā)送過來一個消息,然后BroadcastReceiver收到,再發(fā)送intent去啟動Service。
onBind負責intent的接收。Service的任務就是調(diào)用btEvent這個方法。getApp是一個Activity的方法主要是負責得到那個Activity的Context。
Activity向BroadcastReceiver發(fā)送消息,然后Receiver收到消息之后發(fā)送Intent去Service,Service就會根據(jù)消息作出一些影響UI的行動。Service實際就是一個后臺運行的Activity。
轉(zhuǎn)載于:https://www.cnblogs.com/Tammie/archive/2012/08/10/2632056.html
總結(jié)
以上是生活随笔為你收集整理的Activity、BroadcastReceiver、Service共同工作的简单例子的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用MVPArms框架时,访问网络没响应
- 下一篇: POJ 1986 Distance Qu