如何使用Java Android点击通知后恢复应用程序?

时间:2017-01-26 09:45:45

标签: 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);
        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);
        notificationManager.notify(0, notificationBuilder.build());
//        wl.release();
    }

3 个答案:

答案 0 :(得分:0)

编辑此行

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);

答案 1 :(得分:0)

我这样做并且有效:

在MainAcitivity中我有:

public static Context currentContext;

接下来在onCreate和onResume中的所有活动中执行此操作:

MainActivity.currentContext = this;

接下来当我建立通知时,我会这样做:

Intent intent = new Intent(context, MainActivity.currentContext.getClass());
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);

答案 2 :(得分:0)

这有效;

<activity android:name=".YourActivity" android:launchMode="singleTop">