唤醒锁工作不正常 [英] Wake Lock not working properly

查看:262
本文介绍了唤醒锁工作不正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提前道歉,如果我不是以书面英语不错。
我正在写一个简单的任务的应用程序,提醒我,在特定的时间报警。

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.

下面我设定的报警与AlarmManager:

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);
    }
}

然后AlarmManagerHelper:

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);
    }
}

和AlarmScreenActivity是:

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();
    }
}

与AlarmManagerHelper和AlarmScreenActivity显示它呢。

then with AlarmManagerHelper and AlarmScreenActivity displaying it.

我的问题是:
在具体的时间应该唤醒和振铃不会做整型,所以当我preSS电源按钮,一个转弯屏幕是工作?!
(当处于调试模式和设备,连接到系统正常工作)
我希望完美地描述了我的问题。

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.

推荐答案

AlarmManagerHelper.onReceive 运行系统持有锁(因为报警管理器),将不失败。但 context.startActivity之间(一); 和活动的启动系统又睡着。您需要为使用WakefulBroadcastReceiver(见<一href=\"http://stackoverflow.com/questions/26380534/broadcastreceiver-vs-wakefulbroadcastreceiver\">BroadcastReceiver VS WakefulBroadcastReceiver )或(这是我使用的)一个 WakefulIntentService

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.

在任何情况下,你需要启动服务,并从那里开始您的活动。对于WakefulIntentService格局看我的答案<一个href=\"http://stackoverflow.com/questions/20321444/powermanager-partial-wake-lock-android/20332558#20332558\">PowerManager.PARTIAL_WAKE_LOCK Android的和链接那里。

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.

这篇关于唤醒锁工作不正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆