android之broadcast发送广播
生活随笔
收集整理的這篇文章主要介紹了
android之broadcast发送广播
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
我們有時(shí)會(huì)遇到這樣的情況,當(dāng)手機(jī)處于睡眠狀態(tài)時(shí),到了某個(gè)時(shí)間點(diǎn),我們需要做一些必要的事情。這是如何做到的呢?我們首先會(huì)想到鬧鐘,設(shè)置一個(gè)鬧鐘,到了設(shè)置的時(shí)間點(diǎn),鬧鐘就會(huì)響。當(dāng)然,還有很多其他的應(yīng)用...
下面給出一個(gè)例子,方便學(xué)習(xí)和查閱
BroadcastReceiver
package com.android.test;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.util.Log;
public class TestUpdateReceiver extends BroadcastReceiver{
private static final String TAG = "TestUpdateReceiver";
private static final boolean DEBUG = true;
static final Object mStartingServiceSync = new Object();
static PowerManager.WakeLock mStartingService;
private Context mContext = null;
@SuppressWarnings("deprecation")
@Override
public void onReceive(Context context, Intent intent) {
if(mContext == null) mContext = context;
if(DEBUG){
Log.v(TAG, "====================action=" + intent.getAction());
}
Intent i = new Intent();
i.setClass(mContext, TestUpdateService.class);
i.putExtras(intent);
i.putExtra("action", intent.getAction());
beginStartingService(mContext, i);
}
public static void beginStartingService(Context context, Intent intent) {
synchronized (mStartingServiceSync) {
if (mStartingService == null) {
PowerManager pm =
(PowerManager)context.getSystemService(Context.POWER_SERVICE);
mStartingService = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"StartingAlertService");
mStartingService.setReferenceCounted(false);
}
mStartingService.acquire();
context.startService(intent);
}
}
/**
* Called back by the service when it has finished processing notifications,
* releasing the wake lock if the service is now stopping.
*/
public static void finishStartingService(Service service, int startId) {
synchronized (mStartingServiceSync) {
if (mStartingService != null) {
if (service.stopSelfResult(startId)) {
mStartingService.release();
}
}
}
}
}
同時(shí)啟動(dòng)一個(gè)服務(wù)
package com.android.test;
public class TestUpdateService extends Service{
private static final String TAG = "TestUpdateService";
private static final boolean DEBUG = true;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private volatile Looper mServiceLooper = null;
private volatile Handler mUpdateHandler;
private class UpdateHandler extends Handler{
public UpdateHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
processMessage(msg);
TestUpdateReceiver.finishStartingService(TestUpdateService.this, msg.arg1);
}
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
mContext = this;
HandlerThread thread = new HandlerThread("TestUpdateService");
thread.start();
mServiceLooper = thread.getLooper();
mUpdateHandler = new UpdateHandler(mServiceLooper);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
if(intent != null){
Message msg = mUpdateHandler.obtainMessage();
msg.obj = intent.getExtras();
msg.arg1 = startId;
mUpdateHandler.sendMessage(msg);
}
return START_REDELIVER_INTENT;
}
private void processMessage(Message msg){
Bundle bundle = (Bundle) msg.obj;
String action = bundle.getString("action");
if(DEBUG){
Log.v(TAG, "processMessage(Message msg)=================action=" + action);
}
/*在這里做你要做的事情*/ /*在這里做你要做的事情*/ /*在這里做你要做的事情*/ }
}
原理是先設(shè)一個(gè)鬧鐘,等待鬧鐘發(fā)送廣播,TestUpdateReceiver接收該廣播,并獲得 PowerManager.WakeLock的一個(gè)實(shí)例mStartingService ,并執(zhí)行 mStartingService.acquire(),然后去做你想做的事情,最后執(zhí)行mStartingService . release ( ) 把獲得的lock釋放掉。關(guān)于電源管理的知識(shí),請(qǐng)查閱其他資料。
最后,不要忘了在配置文件里加相應(yīng)的權(quán)限
下面給出一個(gè)例子,方便學(xué)習(xí)和查閱
BroadcastReceiver
同時(shí)啟動(dòng)一個(gè)服務(wù)
原理是先設(shè)一個(gè)鬧鐘,等待鬧鐘發(fā)送廣播,TestUpdateReceiver接收該廣播,并獲得 PowerManager.WakeLock的一個(gè)實(shí)例mStartingService ,并執(zhí)行 mStartingService.acquire(),然后去做你想做的事情,最后執(zhí)行mStartingService . release ( ) 把獲得的lock釋放掉。關(guān)于電源管理的知識(shí),請(qǐng)查閱其他資料。
最后,不要忘了在配置文件里加相應(yīng)的權(quán)限
總結(jié)
以上是生活随笔為你收集整理的android之broadcast发送广播的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: android4.0 系统广播集
- 下一篇: Ubuntu 12.04 下编译Andr