onReceive() method not opening application while device is in sleep mode..?

房东的猫 提交于 2019-12-11 18:56:10

问题


Hi friends i have a problem... Actually i use this code to open my application in certain time..the app is working fine but when the device is in sleep mode not working..??

public class MyBroadCastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context ctx, Intent intent) {
        Intent intent = new Intent(ctx, ActivityMain.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        ctx.startActivity(intent);
    }

}

ActivityMain.java

intent = new Intent(getBaseContext(), MyBroadCastReceiver.class);

        pendingIntent = PendingIntent.getBroadcast(
                getBaseContext(), REQ_CODE, intent, 0);

        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                + (60 * 1000), pendingIntent);

In device sleep mode not working, BroadCastReceiver Calss unable to open the MainActivity class.

I have given permission in menifest

<uses-permission android:name="android.permission.WAKE_LOCK"/>

Any Help please.


回答1:


The wake lock of your broadcast receiver is only guaranteed to keep the phone awake for the duration of the onReceive method. If you need to do work beyond that method, you need to aquire/manage another wake lock.

A Commonsware library exists for this purpose. Have a look at: https://github.com/commonsguy/cwac-wakeful/blob/master/README.markdown



来源:https://stackoverflow.com/questions/14332321/onreceive-method-not-opening-application-while-device-is-in-sleep-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!