我想将我的应用程序发送到睡眠状态,然后在设定的时间将其唤醒.我让它睡觉但不醒来.
这设置了唤醒锁:
private void setWakeLock(){
System.out.println("wakelock");
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "DoNotDimScreen");
wl.acquire();
}
Run Code Online (Sandbox Code Playgroud)
这会为唤醒/睡眠时间设置警报:
private void setWakeSleep(){
java.util.Calendar c = java.util.Calendar.getInstance();
c.set(java.util.Calendar.HOUR_OF_DAY, 17);
c.set(java.util.Calendar.MINUTE, 53);
c.set(java.util.Calendar.MILLISECOND, 0);
Intent sleepIntent = new Intent("SLEEP_INTENT");
PendingIntent sleepPendingIntent = PendingIntent.getBroadcast(this, 0, sleepIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), sleepPendingIntent);
c.set(java.util.Calendar.HOUR_OF_DAY, 18);
c.set(java.util.Calendar.MINUTE, 14);
c.set(java.util.Calendar.MILLISECOND, 0);
Intent wakeIntent = new Intent("WAKE_INTENT");
PendingIntent wakePendingIntent = PendingIntent.getBroadcast(this, 0, wakeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager2 = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager2.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), wakePendingIntent);
}
Run Code Online (Sandbox Code Playgroud)
这是广播接收器:
private BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Time updateHour = new Time();
updateHour.set(System.currentTimeMillis());
if (intent.getAction().equals("SLEEP_INTENT")) {
System.out.println("sleep");
wl.release();
}
if (intent.getAction().equals("WAKE_INTENT")) {
wl.acquire();
System.out.println("wake");
//initialise();
}
}
};
Run Code Online (Sandbox Code Playgroud)
任何帮助非常感谢!
首先,你不需要唤醒锁; 那些是为了防止设备进入睡眠状态,除非你的应用程序确实需要它(它会杀死电池),这是非常反社交的.
其次,如果您在18:14之后调用它,则设置唤醒时间的代码将失败,因为您现在将定义过去的时间.我们暂时忽略它.
接下来,您的意图操作应该是"org.user1797190.WAKE_INTENT",而不是简单的"WAKE_INTENT",这可能会导致冲突.如果您预计将此意图公开,请考虑在http://openintents.org上注册.不过,这也不是你的问题.
您不需要alarmManager2 - 系统中只有一个报警管理器,因此只需重新使用第一个报警管理器.
我从来没有听说过让应用程序本身"睡觉".你是说你希望应用程序消失,然后再回来?
这就是我要做的.完全忘记"SLEEP_INTENT".只需安排一个"WAKE_INTENT",然后调用finish().您的应用只会离开屏幕.
我会完全忘记广播接收器.相反,我会使用getActivity()而不是getBroadcast()来获取将重新启动活动的待定意图.修改您的清单,以便您的WAKE_INTENT将转到该活动.此外,您应该将"android:launchMode"属性设置为"singleTask",这样就不会创建多个活动实例.onNewIntent()如果您的活动在到达时已经在运行,您还需要实现处理唤醒意图.
最后,如果您的活动是将创建意图的同一应用程序的一部分,则根本不需要命名意图; 你可以按类创建它们.你需要另一种方法让接收者知道这是一个唤醒意图.
所以,把它们放在一起:
您的清单应包含:
<activity android:name=".TestActivity" android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
您的代码应包含:
/**
* Arrange for the activity to return at a specific time.
* Call finish() after calling this method().
* This function can be called from anywhere that has a valid Context.
*/
public static void scheduleWakeup(Context ctx, long timeMillis) {
if (DEBUG) Log.d(TAG, "Scheduling wakeup for " + timeMillis);
Intent intent = new Intent(ctx, TestActivity.class);
intent.putExtra("wakeup", true);
PendingIntent pi = PendingIntent.getActivity(ctx, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager mgr = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
mgr.cancel(pi); // Cancel any previously-scheduled wakeups
mgr.set(AlarmManager.RTC_WAKEUP, timeMillis, pi);
}
...
protected void onCreate(Bundle state) {
Intent intent = getIntent();
if (intent.getBooleanExtra("wakeup", false)) {
// We were woken up by the alarm manager
}
...
}
protected void onNewIntent(Intent intent) {
if (intent.getBooleanExtra("wakeup", false)) {
// We were woken up by the alarm manager, but were already running
}
}
Run Code Online (Sandbox Code Playgroud)
这与我在自己的应用程序中所做的非常接近,对我来说效果非常好.
当然,你必须自己测试一下.Log.d()是你的朋友.
| 归档时间: |
|
| 查看次数: |
5425 次 |
| 最近记录: |