点击通知后显示 activity

show activity after click on notification

我正在尝试在用户单击通知后重新激活我的应用程序。我找到了一些例子和解释,但对我没有任何帮助。请在下面查看我的代码。用户可以在 运行 时隐藏该应用程序,以便通知通知 运行 进程。完成后或 运行,我希望用户能够通过单击通知来显示该应用程序。在这种情况下我该怎么做?现在的工作:进程 运行 时带有进度条的通知。但是点击它没有任何反应。

    private void bgTasksStarter(final String doThis, final String thisFileName, final String thisCompPass, final String thisTestStr) {


    String sOut = "";
    switch(doThis) {
        case doEncrypt:
            sOut = lng.encryption;
            break;

        case doDecrypt:
            sOut = lng.decryption;
            break;
    }

    final String fsOut = sOut;

    final int id = 1;

    final NotificationManager mNotifyManager;
    final Builder mBuilder;

    mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setContentTitle(thisFileName)
        .setContentText(fsOut + " " + lng.isRunning)
        .setSmallIcon(R.drawable.ic_launcher);

    new Thread(
            new Runnable() {
                @Override
                public void run() {

                    bgTasks = new BgTasks();
                    bgTasks.execute(doThis, thisFileName, thisCompPass, thisTestStr);

                    mBuilder.setProgress(0, 0, true);
                    mNotifyManager.cancel(id);
                        mNotifyManager.notify(id, mBuilder.build());

                    while (busy) {

                        try {
                                Thread.sleep(1*1000);
                            } catch (InterruptedException e) {
                                Log.d("xxx", "sleep failure");
                            }
                    }
                        mBuilder.setContentText(fsOut + " " + lng.isFinished)
                            .setProgress(0,0,false);
                        mNotifyManager.notify(id, mBuilder.build());

                }
            }
        ).start();
}

您应该像这样通过 pendingIntent 添加一个意图:

Intent intent = new Intent(context, desiredClass.class);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(pendingIntent);

注意标志 FLAG_UPDATE_CURRENT 并检查它是否适合您的情况。

您需要使用 setContentIntent 方法,该方法将在您点击通知时触发待定意图。

Intent launcher = new Intent(context, YourActivity.class);
launcher.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

PendingIntent contentIntent = PendingIntent.getActivity(context, 0, launcher, PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(launcher);