【发布时间】:2015-03-10 08:07:59
【问题描述】:
点击推送notification 后,我正在尝试打开Activity 并通过HttpPost 下载文件。我是Android Programming 的新手。我不确定我应该把通过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());
}
}
【问题讨论】:
-
我可以看到你已经在点击通知时开始了一个新的活动,现在你想要什么,????
-
您希望代码下载文件,您将放入 MainPage.class ???
-
嗨拉胡尔,感谢您的回复。单击通知时,我已经编写了代码以转到另一个名为“MainPage”的活动。但我想要的是在哪里放置代码以通过 HttpPost 下载图像并显示它何时到达“MainPage”活动?
-
我自己想出来的
标签: android android-intent push-notification