如何设置设备上的屏幕打开,并在收到通知时振动?
How to set that screen on device is on, and vibrate when notification comes?
您好,我正在构建一个使用通知的应用程序。我希望当收到通知时,我的设备会振动,并且如果屏幕处于睡眠模式,那么当通知到来时屏幕会打开。
这是我的通知生成器:
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.asp)
.setContentText(notificationData.getText())
.setContentTitle(notificationData.getTitle())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setLights(Color.BLUE, 3000, 3000)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt(), notification.build());
当收到通知时,我应该如何设置振动和设备屏幕亮起?
在通知生成器上振动设备 setVibrate(long[])。
long[] vibrate = {500,200,200,500};
notification.setVibrate(vibrate);
要打开设备屏幕,请尝试使用以下代码。
public static void turnScreenOn(int sec, final Context context)
{
final int seconds = sec;
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
if( !isScreenOn )
{
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
wl.acquire(seconds*1000);
WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");
wl_cpu.acquire(seconds*1000);
}
}
别忘了添加 WAKE_LOCK
权限。
<uses-permission android:name="android.permission.WAKE_LOCK" />
您好,我正在构建一个使用通知的应用程序。我希望当收到通知时,我的设备会振动,并且如果屏幕处于睡眠模式,那么当通知到来时屏幕会打开。 这是我的通知生成器:
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.asp)
.setContentText(notificationData.getText())
.setContentTitle(notificationData.getTitle())
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setLights(Color.BLUE, 3000, 3000)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(new Random().nextInt(), notification.build());
当收到通知时,我应该如何设置振动和设备屏幕亮起?
在通知生成器上振动设备 setVibrate(long[])。
long[] vibrate = {500,200,200,500};
notification.setVibrate(vibrate);
要打开设备屏幕,请尝试使用以下代码。
public static void turnScreenOn(int sec, final Context context)
{
final int seconds = sec;
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
if( !isScreenOn )
{
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
wl.acquire(seconds*1000);
WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");
wl_cpu.acquire(seconds*1000);
}
}
别忘了添加 WAKE_LOCK
权限。
<uses-permission android:name="android.permission.WAKE_LOCK" />