在Push Notification Android上下载文件

案头见蠹鱼,犹胜凡俦侣。这篇文章主要讲述在Push Notification Android上下载文件相关的知识,希望能为你提供帮助。
我正在尝试打开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()); }}

答案当你在评论中点击时,你想要下载图像并在MainPage Activity中显示它
I want is where to put the code to download an image via HttpPost and show when it reaches the 'MainPage' activity
如果主要目的是在活动中显示图像,您可以通过将图像URL传递给活动来完成
那么你可以像这样使用毕加索这样的库
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);

希望这个帮助
另一答案如果您不需要显示推送通知,只需在推送通知上下载图像。
几乎没有相同的库。您可以使用活动总线并获取活动中收到的推送通知事件。比之后你可以在主Activity中下载图像。 qazxsw poi
【在Push Notification Android上下载文件】同样,您也可以显示通知并将事件发送到MainActivity以下载图像并显示它。

    推荐阅读