案头见蠹鱼,犹胜凡俦侣。这篇文章主要讲述在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以下载图像并显示它。
推荐阅读
- 未找到Android活动异常(找不到处理Intent的活动)
- 差异以及何时使用getApplication(),getApplicationContext(),getBaseContext()和someClass.this
- 通过Electron Framework掌握剪贴板的使用
- 如何使用Electron Framework在OS的文件浏览器中显示和聚焦文件(或文件夹)
- 如何在Electron Framework中连接到MySQL数据库
- 如何在Electron Framework中的渲染器进程内执行主进程的功能
- 如何在Electron Framework中提取.zip文件的内容(解压缩)
- 如何在Electron Framework中获取屏幕的宽度和高度
- 如何在Electron Framework中使用最小化,最大化和关闭控件创建自定义无框架窗口(无标题栏)