仅在通知选项卡上安装应用程序

时间:2016-11-10 11:16:55

标签: android android-notifications notificationmanager

我在下载应用程序后向用户显示通知。

我还有一个installApk方法,用于安装下载的应用程序,但在代码段(completeNotification)中,应用程序会自动安装。我需要它只在用户标记通知时安装。

我忽略了什么吗?

感谢您的帮助。

completeNotification方法:

public void completeNotification() {
        Intent install = installApk(urlPath, context, mNotificationManager,
                NOTIFYCATIONID);
        PendingIntent pending = PendingIntent.getActivity(context, 0, install, 0);

        mBuilder = new NotificationCompat.Builder(context)
                .setContentTitle(appName)
                .setContentText("ready to install.");
        mBuilder.setContentIntent(pending);
        mBuilder.setSmallIcon(R.drawable.placeholder);
        mBuilder.setDefaults(Notification.DEFAULT_SOUND);
        mBuilder.setAutoCancel(true);
        mNotificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(1, mBuilder.build());
    }

installApk方法:

public static Intent installApk(String urlPath, Context context,
                                    NotificationManager mNotificationManager, int NOTIFYCATIONID) {
        Intent apkIntent = new Intent();
        apkIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        apkIntent.setAction(android.content.Intent.ACTION_VIEW);
        File apkFile = new File(urlPath);
        Uri uri = Uri.fromFile(apkFile);
        apkIntent
                .setDataAndType(uri, "application/vnd.android.package-archive");
        context.startActivity(apkIntent);
        mNotificationManager.cancel(NOTIFYCATIONID);
        return apkIntent;
    };

1 个答案:

答案 0 :(得分:0)

删除此行:

context.startActivity(apkIntent);