我的應用程序描述如下:應用程序將從服務器接收推送信息並顯示在android的通知欄上。當我點擊應用程序的通知時,會加載我從服務器獲得的URL。但現在我的問題是,我無法將從服務器收到的網址發送到原始活動。將數據從服務發送到活動時出錯
下面是我運行應用程序時得到的錯誤。
04-09 11:25:26.503: E/AndroidRuntime(1030): FATAL EXCEPTION: IntentService[GCMIntentService-810012644757-1]
04-09 11:25:26.503: E/AndroidRuntime(1030): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
04-09 11:25:26.503: E/AndroidRuntime(1030): at android.app.ContextImpl.startActivity(ContextImpl.java:624)
04-09 11:25:26.503: E/AndroidRuntime(1030): at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
04-09 11:25:26.503: E/AndroidRuntime(1030): at com.ketan.demo.GCMIntentService.generateNotification(GCMIntentService.java:118)
04-09 11:25:26.503: E/AndroidRuntime(1030): at com.ketan.demo.GCMIntentService.onMessage(GCMIntentService.java:70)
04-09 11:25:26.503: E/AndroidRuntime(1030): at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:179)
04-09 11:25:26.503: E/AndroidRuntime(1030): at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
04-09 11:25:26.503: E/AndroidRuntime(1030): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 11:25:26.503: E/AndroidRuntime(1030): at android.os.Looper.loop(Looper.java:130)
04-09 11:25:26.503: E/AndroidRuntime(1030): at android.os.HandlerThread.run(HandlerThread.java:60)
我的代碼:
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_stat_gcm;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent().setClass(context.getApplicationContext(), DemoActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
notificationIntent.putExtra("URL", message);
// Launch the new activity and add the additional flags to the intent
context.getApplicationContext().startActivity(notificationIntent);
/*Intent i = new Intent().setClass(context.getApplicationContext(), DemoActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("URL", message);
// Launch the new activity and add the additional flags to the intent
context.getApplicationContext().startActivity(i);*/
}
活動演示
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkNotNull(SERVER_URL, "http://10.0.2.2:8080/gcm-demo-server/");
// checkNotNull(SENDER_ID, "972801588344");
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
GCMRegistrar.checkManifest(this);
setContentView(R.layout.main);
Bundle extras = getIntent().getExtras();
if (extras != null) {
url = extras.getString("URL");
}
web=(WebView) findViewById(R.id.webView1);
if(url==null)
{
web.loadUrl("http://kqxs.vn/trang_chu");
}else
{
web.loadUrl(url);
}
}
我固定的
private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_stat_gcm;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
Intent notificationIntent = new Intent(context, DemoActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.putExtra("URL", message);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
以下但我有一個問題,這是唯一的應用程序,我加載正確的網址第一次,從第二次,從服務器收到一個新的URL後,但我的應用程序加載的URL的第一次。我已經試了又試很多次,但仍然獲得結果,因此...