当设备处于休眠状态时,不会调用IntentService

时间:2015-06-04 11:09:47

标签: android broadcastreceiver android-service intentservice wakelock

我从WakefullBroadcastReceiver

触发IntentService
public class MyService extends IntentService {
private static final String NAME = MyService.class.getName() + ".Lock";
private static volatile WakeLock lockStatic = null;

synchronized public 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 MyService(String name) {
    super(name);
}

public MyService() {
    super("MyService");
}

@Override
protected void onHandleIntent(Intent intent) {
    try {
        Log.i("IntentService", "In HandleIntent");
    } finally {

    }

}

}

以上所有日志均为“显示”。但我的服务没有被调用。

IntentService

std::pair<int,int>

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

在onCreate()中,您需要将标志添加到窗口中。例如:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
      getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);`
}

我已经在这里发布了check this