这是我使用的代码BroadCastReceiver
:
public class SMSBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// SMSReceivedService is my custom service, extends from Service class
Intent myIntent = new Intent(context, SMSReceivedService.class);
myIntent.putExtras(intent);
WakefulIntentService.sendWakefulWork(context, myIntent);
}
}
public class SMSReceivedService extends extends WakefulIntentService {
public SMSReceivedService(String name) {
super(name);
}
@Override
protected void doWakefulWork(Intent intent) {
// main code here
}
}
这是里面的一段代码WakefulIntentService
synchronized private static PowerManager.WakeLock getLock(Context context) {
if (lockStatic == null) {
PowerManager mgr =
(PowerManager)context.getSystemService(Context.POWER_SERVICE);
lockStatic=mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, NAME);
lockStatic.setReferenceCounted(true);
}
return(lockStatic);
}
public static void sendWakefulWork(Context ctxt, Intent i) {
getLock(ctxt.getApplicationContext()).acquire();
ctxt.startService(i);
}
用过的人WakefullIntentService
都知道,只要打电话sendWakefulWork
,WakefullIntentService
就会在后台做所有的事情。但在上面的代码中,WakefullIntentService
只是保持唤醒锁,但完成后,我看不到任何释放此锁的代码。是真的吗?所以它会影响Android用户。请给我一些想法。