2

私のアプリケーションは次のように記述されています: アプリケーションはサーバーからプッシュ情報を受け取り、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 を受信した後、2 回目から初めて正しい URL をロードする唯一のアプリケーションですが、私のアプリケーションは初めて URL をロードしました。何度も挑戦したのに結果が出てしまったので…

4

1 に答える 1

0

次の質問を参照してください。

Android:インテントの setFlags と addFlags の違いは何ですか

Intent.setFlags :インテントに新しいフラグを追加して、古いフラグを置き換えていることを意味します

Intent.addFlags :すでにインテントに追加された新しいフラグを追加していることを意味します

そのため、次のようにIntent.FLAG_ACTIVITY_NEW_TASK フラグをインテントに追加するnotificationIntent.addFlags 代わりに使用する必要があります。notificationIntent.setFlagsnotificationIntent

.......
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);
.......
于 2013-04-09T04:49:41.320 に答える