使用Android如何开始从通知抽屉里的APK下载(Android how to start an

2019-10-18 03:40发布

我有一个应用程序,我可以通过GCM上推送通知。 我收到的通知抽屉里的通知,但是当我点击它,从应用程序的活动开始(EntryActivity)。 我想发生的事情是,如果在通知中的消息是URL到.apk文件如“ HTTP://path_to_file.apk ”,然后我想开始下载文件。

下面是生成通知的代码,是不是我需要添加或更改开始下载?

/**
     * Issues a notification to inform the user that server has sent a message.
     */
    private static void generateNotification(Context context, String message) {
        int icon = R.drawable.ic_launcher;
        long when = System.currentTimeMillis();
        NotificationManager notificationManager = (NotificationManager)
                context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notification = new Notification(icon, message, when);

        String title = context.getString(R.string.app_name);


        Intent notificationIntent = new Intent(context, EntryActivity.class);

        // set intent so it does not start a new activity
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
                Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent intent =
                PendingIntent.getActivity(context, 0, notificationIntent, 0);
        notification.setLatestEventInfo(context, title, message, intent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        // Play default notification sound
        notification.defaults |= Notification.DEFAULT_SOUND;

        // Vibrate if vibrate is enabled
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);     

    }

Answer 1:

尝试这个:

    Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://path_to_file.apk"));
    notificationIntent.setClassName("com.android.browser",
                                "com.android.browser.BrowserActivity");


文章来源: Android how to start an apk download from the notification drawer