在Push Notification Android上下载文件

时间:2015-03-10 08:08:00

标签: android android-intent push-notification

点击推送Activity后,我尝试打开HttpPost并通过notification下载文件。我是Android Programming的新手。我不知道我应该在下面通过HttpPost下载文件的代码?

我的BroadcastReceiver:

public class MyGcmBroadcastReceiver extends WakefulBroadcastReceiver {
public void onReceive(Context context, Intent intent) { 
    ComponentName comp = new ComponentName(context.getPackageName(), GcmIntentService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
}
}

我的意向服务课程:

    public class GcmIntentService extends IntentService{
    public static final String TAG="TEST";
    public static final int NOTIFICATION_ID = 1;
    private NotificationManager mNotificationManager;
    NotificationCompat.Builder builder;
    Bundle extras;
     public GcmIntentService()
        {
            super("GcmIntentService");
        }

     @Override
        protected void onHandleIntent(Intent intent) {
         extras = intent.getExtras();
         GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
         String messageType = gcm.getMessageType(intent);


         if (!extras.isEmpty()) { 

             if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
                 sendNotification("Send error: " + extras.toString());
             } else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_DELETED.equals(messageType)) {
                    sendNotification("Deleted messages on server: " +
                            extras.toString());
                // If it's a regular GCM message, do some work.
                } else if (GoogleCloudMessaging.
                        MESSAGE_TYPE_MESSAGE.equals(messageType)) {
                    // This loop represents the service doing some work.
                    for (int i=0; i<5; i++) {
                        Log.i(TAG, "Working... " + (i+1)
                                + "/5 @ " + SystemClock.elapsedRealtime());
                        try {
                            Thread.sleep(5000);
                        } catch (InterruptedException e) {
                        }
                    }
                    Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());
                    // Post notification of received message.
                    sendNotification(extras.toString());
                    Log.i(TAG, "Received: " + extras.toString());
                }


         }
         MyGcmBroadcastReceiver.completeWakefulIntent(intent);
     }

         private void sendNotification(String msg) {
                mNotificationManager = (NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);

                PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, MainPage.class), 0);

                NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("New Message")
                .setSmallIcon(R.drawable.ic_launcher)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(extras.getString("Message:").substring(extras.getString("Message:").indexOf(":")+2, extras.getString("Message:").length())))
                .setContentText(extras.toString().substring((extras.toString().indexOf("="))+1, extras.toString().indexOf("_")));

                mBuilder.setContentIntent(contentIntent);
                mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
            }


}

2 个答案:

答案 0 :(得分:0)

在评论中您想要下载图像并在主页活动中显示

I want is where to put the code to download an image via HttpPost and show when it reaches the 'MainPage' activity

如果主要目的是在活动中显示图像,您可以通过将图片网址传递给活动

来实现

那么你可以像这样使用Picasso这样的库

 Picasso.with(context).load(url).into(imageView);

和Picasso为您做请求并在ImageView中显示图像

有关毕加索的更多信息,请查看:

Picasso - A powerful image downloading and caching library for Android

但如果您要下载图片并将其保存在设备本地

您可以在onHandleIntent()

上执行此操作
     @Override
     protected void onHandleIntent(Intent intent) {
     extras = intent.getExtras();
     GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
     String messageType = gcm.getMessageType(intent);
     /***HERE***/
     downloadImage(extras);
     ....
     }

然后将URI传递给活动并显示您可以使用picasso的图像

Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);

希望这个帮助

答案 1 :(得分:0)

如果您不需要显示推送通知,只需在推送通知上下载图像。

很少有相同的库。您可以使用活动总线获取活动中收到的推送通知事件。比之后你可以在主Activity中下载图像。 https://github.com/greenrobot/EventBus

同样,您也可以显示通知并将事件发送到MainActivity以下载图像并显示它。