下载推送通知Android上的文件

下载推送通知Android上的文件,android,android-intent,push-notification,Android,Android Intent,Push Notification,我正在尝试打开一个活动并在单击推送通知后通过HttpPost下载一个文件。我不熟悉安卓编程。我不确定通过下面的HttpPost下载文件的代码应该放在哪里 我的广播接收器: public class MyGcmBroadcastReceiver extends WakefulBroadcastReceiver { public void onReceive(Context context, Intent intent) { ComponentName comp = new Compone

我正在尝试打开一个
活动
并在单击推送
通知
后通过
HttpPost
下载一个文件。我不熟悉安卓编程。我不确定通过下面的
HttpPost
下载文件的代码应该放在哪里

我的广播接收器:

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());
            }


}
公共类gcminentservice扩展了IntentService{
公共静态最终字符串TAG=“TEST”;
公共静态最终整数通知_ID=1;
私人通知经理通知经理;
通知建筑商;
捆绑附加;
公共GCMinentService()
{
超级(“GCMinentService”);
}
@凌驾
受保护的手部内容无效(意图){
extras=intent.getExtras();
GoogleCloudMessaging gcm=GoogleCloudMessaging.getInstance(this);
字符串messageType=gcm.getMessageType(intent);
如果(!extras.isEmpty()){
if(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)){
sendNotification(“发送错误:+extras.toString());
}否则如果(谷歌云信息)。
消息类型已删除。等于(消息类型)){
sendNotification(“服务器上已删除的邮件:”+
附加的toString());
//如果是常规GCM消息,请执行一些操作。
}否则如果(谷歌云信息)。
MESSAGE_TYPE_MESSAGE.equals(messageType)){
//此循环表示正在执行某些工作的服务。

对于(int i=0;i当您在评论中监视时,您希望下载图像并在主页活动中显示它

I want是通过HttpPost下载图像并在到达“主页”活动时显示的代码所在位置

如果主要目的是在活动中显示图像,则可以通过将图像url传递给活动来实现

那个么你们可以像毕加索这样使用图书馆

 Picasso.with(context).load(url).into(imageView);
毕加索会按照你的要求在ImageView中显示图像

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

但是,如果您打算下载图片并将其保存在本地设备中

您可以在
onHandleIntent()上执行此操作

然后将URI传递给活动,并显示可以使用毕加索进行此操作的图像

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

如果您不需要显示推送通知,只需下载推送通知时的图像,请期待此帮助

相同的库很少。您可以使用事件总线获取活动中接收到的pushnotification事件。之后,您可以在主活动中下载映像。


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

我可以看到您已经在通知上开始了一个新的活动,单击“现在您到底想要什么?”?您想要下载文件的代码,并将其放入MainPage.class???嗨,Rahul,谢谢您的回复。我已经为您编写了代码单击通知时转到另一个名为“主页”的活动。但我想要的是将代码放在哪里,以便通过HttpPost下载图像,并在图像到达“主页”活动时显示?我自己解决了这个问题
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);