这是我设置通知的代码,它可以运行:
@Override
public void onReceive(Context context, Intent intent) {
category = (String) intent.getExtras().get("CATEGORY");
notes = (String) intent.getExtras().get("NOTES");
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
context).setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(category).setContentText(notes);
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
这是我的BroadcastReceiver的代码片段。每当调用此BroadcastReceiver时,它都会在状态栏中显示通知。在我的调试过程中,我注意到当屏幕关闭并发出新通知时,屏幕将无法开启。有没有办法做到这一点?无论何时发出新通知并且屏幕关闭,都应该打开一段时间。模拟就像收到一个新的短信。
答案 0 :(得分:3)
试试这个:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire(3000);
wl.release();
答案 1 :(得分:0)
实际上,如果您只想在收到通知时激活屏幕,请在创建通知后使用此代码:
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "Tag");
wakeLock.acquire();
wakeLock.release();
答案 2 :(得分:0)
createNotification(); //your implementation
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = Build.VERSION.SDK_INT >= 20 ? pm.isInteractive() : pm.isScreenOn(); // check if screen is on
if (!isScreenOn) {
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "myApp:notificationLock");
wl.acquire(3000); //set your time in milliseconds
}