2013-05-10 59 views
1

我有一個應用程序,我可以通過GCM推送通知。我收到通知抽屜中的通知,但是當我點擊它時,應用程序中的活動就開始了(EntryActivity)。我想要發生的是,如果通知中的消息是.apk文件的url,例如'http://path_to_file.apk',那麼我希望該文件開始下載。Android如何從通知抽屜啓動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);  

    } 

回答

0

試試這個:

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

感謝,完美的作品。 – turtleboy 2013-05-10 09:49:57