我们有时会遇到这样的情况,当手机处于睡眠状态时,到了某个时间点,我们需要做一些必要的事情。这是如何做到的呢?我们首先会想到闹钟,设置一个闹钟,到了设置的时间点,闹钟就会响。当然,还有很多其他的应用...
下面给出一个例子,方便学习和查阅
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();
-
}
-
}
-
}
-
}
-
-
}
同时启动一个服务
- 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);
-
}
-
/*在这里做你要做的事情*/
- /*在这里做你要做的事情*/
- /*在这里做你要做的事情*/
-
-
}
-
-
}
原理是先设一个闹钟,等待闹钟发送广播,TestUpdateReceiver接收该广播,并获得
PowerManager.WakeLock的一个实例mStartingService
,并执行 mStartingService.acquire(),然后去做你想做的事情,最后执行mStartingService
.release()把获得的lock释放掉。关于电源管理的知识,请查阅其他资料。
最后,不要忘了在配置文件里加相应的权限
阅读(4383) | 评论(0) | 转发(0) |