0

我從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 Wakelock"); 
    lock.acquire(); 
    Log.i("StartService","After Aquire"); 
    context.startService(i);  
    lock.release(); 
    Log.i("StartService","After Release"); 

越來越顯示上述所有日誌觸發的IntentService。但我的服務沒有被調用。

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 { 

    } 

} 

} 

誰能幫助我?

+2

你有沒有在清單文件中聲明你的服務? – Kartheek

+0

如果您實際使用的是'WakefulBroadcastReceiver',並且您按照文檔使用它,則不需要您自己的'WakeLock'。 – CommonsWare

+0

@Kartheek是的,而當設備是活動服務被稱爲 – user3616287

回答

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

相關問題