从服务向活动发送数据时出错

时间:2013-04-09 04:44:09

标签: android push-notification google-cloud-messaging

我的应用程序描述如下:应用程序将从服务器接收推送信息并显示在android的通知栏上。当我点击应用程序的通知时,将加载我从服务器获取的URL。但我现在的问题是,我无法将从服务器收到的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后,从第二次加载正确的URL,但我的应用程序第一次加载了url。我已经多次尝试过,但仍然得到了结果......

1 个答案:

答案 0 :(得分:0)

请参阅以下问题:

Android:What is difference between setFlags and addFlags for intent

Intent.setFlags :表示您通过向intent添加新标志来替换旧标志

Intent.addFlags :表示您正在添加已添加到意图

的新标记

因此,您需要使用notificationIntent.addFlags代替notificationIntent.setFlags将Intent.FLAG_ACTIVITY_NEW_TASK标记附加到notificationIntent意图:

.......
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);                  
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
        Intent.FLAG_ACTIVITY_SINGLE_TOP);
.......