2

残念ながら、他の StackOverflow の投稿でこの問題の解決策を見つけることができなかったため、この質問を再投稿することをお詫びします。基本的に、通知を作成する AppWidget があります。ユーザーに通知をクリックしてアクティビティを起動してもらいたい。AppWidget とアクティビティは同じ APK にあります。

次のコードはすべて AppWidget にあります。通知をクリックすると、AppWidget によって受信されるカスタム インテントがブロードキャストされます。

問題は、カスタム インテントが実際にブロードキャストされたことを logcat が示しているにもかかわらず、アクティビティが起動していないように見えることです。唯一の手がかりは、投稿のタイトルで繰り返される logcat メッセージです。

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(Constants.BROWSER_INTENT)) {
        launchActivity(context, R.layout.appwidget, R.id.launch_button, Activity.class);
    }
    super.onReceive(context, intent);
}


private void displayNotification(Context context, String ticker, String title, String msg) {
    Intent intent = new Intent(context, ThisWidget.class);
    intent.setAction(Constants.BROWSER_INTENT); 
    PendingIntent pending = PendingIntent.getActivity(context, 0, intent, 0);

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.icon, ticker, System.currentTimeMillis());

    notification.setLatestEventInfo(context, title, msg, pending);
    notificationManager.notify(1, notification);
}

いくつかのメモ:

  • ボタンを押して launchActivity() を実行すると機能します (アクティビティを起動します)
  • Android ライフサイクル ドキュメントで推奨されているように、launchActivity は Intent.FLAG_ACTIVITY_NEW_TASK を使用します。

通知プレスからアクティビティを起動するときにこの問題を解決した人はいますか?

4

1 に答える 1