Android 当设备处于睡眠状态时,不会调用IntentService
我正在从WakefullBroadcastReceiver触发IntentServiceAndroid 当设备处于睡眠状态时,不会调用IntentService,android,broadcastreceiver,android-service,intentservice,wakelock,Android,Broadcastreceiver,Android Service,Intentservice,Wakelock,我正在从WakefullBroadcastReceiver触发IntentService Intent i = new Intent(context, MyService.class); i.putExtras(bundle); Log.i("StartService","Before Wakelock"); WakeLock lock = MyService.getLock(context); Log.i("StartService","After Wa
Intent i = new Intent(context, MyService.class);
i.putExtras(bundle);
Log.i("StartService","Before Wakelock");
WakeLock lock = MyService.getLock(context);
Log.i("StartService","After Wakelock");
lock.acquire();
Log.i("StartService","After Aquire");
context.startService(i);
lock.release();
Log.i("StartService","After Release");
将显示以上所有日志。但是我的服务没有被呼叫
意向服务
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 {
}
}
}
有人能帮我吗???在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);`
}
我已经在这里发布了我的服务您是否在清单文件中声明了您的服务?如果您实际使用的是
WakefulBroadcastReceiver
,并且您按照文档使用它,您不需要自己的WakeLock
@Kartheek Yes,当设备处于活动状态时,服务被调用。即使设备处于睡眠状态,您是否要唤醒该设备。@Kartheek,即使设备处于睡眠/唤醒状态,我也要启动该服务