点击通知后显示活动

时间:2016-02-14 15:38:34

标签: android notifications resume

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

    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();
}

2 个答案:

答案 0 :(得分:1)

您应该通过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并检查它是否适合您的情况。

答案 1 :(得分:1)

您需要使用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);