[英]How to turn on screen when new notification is sent in the status bar?
這是我設置通知的代碼,它有效:
@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 時,它都會在狀態欄中顯示通知。 在調試過程中,我注意到當屏幕關閉並發出新通知時,屏幕不會打開。 有沒有辦法做到這一點? 每當發出新通知並且屏幕關閉時,它應該打開一段時間。 模擬就像收到一條新短信。
試試這個:
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
wl.acquire(3000);
wl.release();
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
}
實際上,如果您只想在收到通知時激活屏幕,請在創建通知后使用此代碼:
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.