0

这是我使用的代码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都知道,只要打电话sendWakefulWorkWakefullIntentService就会在后台做所有的事情。但在上面的代码中,WakefullIntentService只是保持唤醒锁,但完成后,我看不到任何释放此锁的代码。是真的吗?所以它会影响Android用户。请给我一些想法。

4

1 回答 1

2

要么你使用 my WakefulIntentService,要么你发明了自己的同名。如果您使用的是 my WakefulIntentService,则锁定已释放onHandleIntent(),您可以通过阅读源代码来判断。

于 2013-05-11T13:52:42.843 回答