Wake Lock not working properly

十年热恋 提交于 2019-12-11 18:41:25

问题


I apologies in advance if I'm not good in writing English. I'm writing a simple task app that remind me with alarm in specific time.

Below I set alarm with AlarmManager :

private static void setAlarm(Context context, Calendar calendar,
        PendingIntent pIntent) {
    AlarmManager alarmManager = (AlarmManager) context
        .getSystemService(Context.ALARM_SERVICE);
    if (android.os.Build.VERSION.SDK_INT >= 
            android.os.Build.VERSION_CODES.KITKAT) {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), pIntent);
    } else {
        alarmManager.set(AlarmManager.RTC_WAKEUP,
            calendar.getTimeInMillis(), pIntent);
    }
}

and then AlarmManagerHelper :

public class AlarmManagerHelper extends BroadcastReceiver {

    public void onReceive(Context context, Intent intent) {
        String title = intent.getStringExtra("Title");
        int hour = intent.getIntExtra("Hour", 0);
        int min = intent.getIntExtra("Minute", 0);
        String alarmTone = intent.getStringExtra("AlarmTone");
        Intent i = new Intent();
        i.setClassName("com.example.tasks",
            "com.example.tasks.AlarmScreenActivity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("Title", title);
        i.putExtra("Hour", hour);
        i.putExtra("Minute", min);
        i.putExtra("AlarmTone", alarmTone);
        context.startActivity(i);
    }
}

and AlarmScreenActivity is:

public class AlarmScreenActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // get intent
        pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Tag");
        wl.acquire();
        mPlayer = new MediaPlayer();
        try {
            if (task_Tone != null && !task_Tone.equals("")) {
                android.net.Uri toneUri = android.net.Uri.parse(task_Tone);
                if (toneUri != null) {
                    mPlayer.setDataSource(this, toneUri);
                    mPlayer.setAudioStreamType(AudioManager.STREAM_ALARM);
                    mPlayer.setLooping(true);
                    mPlayer.prepare();
                    mPlayer.start();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // some code
    public void onClickDissmis(View view) {
        mPlayer.stop();
        finish();
    }

    protected void onDestroy() {
        super.onDestroy();
        wl.release();
    }
}

then with AlarmManagerHelper and AlarmScreenActivity displaying it.

my problem is: in the specific time that should wake up and ringing not do int, so when I press power button an turn screen on that is work???! (when is in debug mode and the device , connected to system work properly) I hope that describe my problem perfectly.


回答1:


I don't understand your problem, exactly. I can say, though, that Android only guarantees that it is holding a wakelock while it delivers the Broadcast. Your code leave considerable time between the reception of the Broadcast, by the Receiver, and the time you seize the wakelock. There is nothing to prevent the device from going back to sleep, in that interval.




回答2:


While AlarmManagerHelper.onReceive runs the system holds a lock (because of the Alarm manager) that will not fail. But between the context.startActivity(i); and the starting of the activity the system falls asleep again. You need to either use a WakefulBroadcastReceiver (see BroadcastReceiver Vs WakefulBroadcastReceiver) or (that's what I use) a WakefulIntentService.

In any case you need to start a service and start your activity from there. For the WakefulIntentService pattern see my answer PowerManager.PARTIAL_WAKE_LOCK android and links there.



来源:https://stackoverflow.com/questions/27903534/wake-lock-not-working-properly

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