Java Android在锁定屏幕上显示通知

时间:2017-01-25 12:27:00

标签: java android

我不知道如何在锁定屏幕上显示通知。这是我的通知:

public static void getSynchronizeNotification(Context context, DataResponse dataResponse) {
    Bitmap Largeicon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(NOTIFICATION_SYNCHRONIZE_ID);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
Notification notification = new Notification.Builder(context)
        .setContentTitle(context.getString(R.string.app_name))
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pendingIntent)
        .setContentText("Zmiany po synchronizacji...")
        .setStyle(new Notification.BigTextStyle().bigText(buildNotificationAfterSynchronizeText(dataResponse)))
        .setLargeIcon(Largeicon)
        .setAutoCancel(true)
        .setVibrate(vibratePattern)
        .setSound(alarmSound)
        .setPriority(Notification.PRIORITY_MAX)
        .build();
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_SYNCHRONIZE_ID, notification);
}

1 个答案:

答案 0 :(得分:0)

我这样做并且有效: 在Manifest中添加:

  <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

 public static void getSynchronizeNotification(Context context, DataResponse dataResponse) {

        Bitmap Largeicon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(NOTIFICATION_SYNCHRONIZE_ID);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
        KeyguardManager km = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
        final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("IN");
        kl.disableKeyguard();

        PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "SMOK Komunal");
        wl.acquire();
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
                .setCategory(Notification.CATEGORY_PROMO)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("Zmiany po synchronizacji...")
                .setStyle(new NotificationCompat.BigTextStyle().bigText(buildNotificationAfterSynchronizeText(dataResponse)))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setLargeIcon(Largeicon)
                .setContentIntent(pendingIntent)
                .setVibrate(vibratePattern)
                .setLights(Color.GREEN, 2000, 2000)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

        wl.release();

        notificationManager.notify(0, notificationBuilder.build());
    }
相关问题