私はAndroidアプリケーションに取り組んでいます。要約すると、このアプリケーションにはユーザーと対話するための UI があり、リモート サービスとも対話します。リモート サービスは Android ランチャー バーに通知を追加し、この通知により UI を再表示できます。アプリとサービスは同じパッケージです。サービス通知機能のコード:
private void showNotification (String contentText) {
Intent intent = new Intent (this, my_app.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mNotification = new Notification.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(ICON)
.setWhen(System.currentTimeMillis())
.setContentTitle(CONTENT_TITLE)
.setContentText(contentText)
.setAutoCancel(false)
.setOngoing(true)
.build();
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
}
注:これはメソッドの現在のバージョンです...以前は次のFLAGSも試しました
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
アプリケーションのマニフェストには、次の構成が含まれています。
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:allowBackup="true"
android:launchMode="singleTop"
android:uiOptions="splitActionBarWhenNarrow"
android:allowTaskReparenting="true">...</application>
注:他のプロパティも試しましたが、これらは影響を期待していません...
アプリケーションを起動しても表示されなくなった場合、ランチャーバーの通知または「アプリケーション一覧」のアプリケーションエントリを使用して再表示することができますが、次のシナリオを実行すると、UI が 2 回表示されます。
- リストからアプリケーションを起動する
- ホームボタンのテープ
- 通知を使用してアプリケーションを (再) 表示する ==> UI が正しく再表示される
- ホームボタンのテープ
- 「applications list」を使用してアプリケーションを (再) 表示する ==> UI の新しいインスタンスが表示される
シナリオのログ:
#start from applications list
I/ActivityManager( 504): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.domain.my_app/.My_App bnds=[40,833][200,1033]} from pid 871
I/my_app( 6915): onCreate
I/my_app::Service( 6930): onCreate
I/my_app( 6915): onResume
#tape the home button
I/my_app( 6915): onPause
#restart from notification in launcher bar
I/ActivityManager( 504): START u0 {flg=0x34400000 cmp=com.domain.my_app/.My_App bnds=[0,102][720,230]} from pid -1
I/my_app( 6915): onResume
#tape the home button
I/my_app( 6915): onPause
#start from application list
I/ActivityManager( 504): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.domain.my_app/.My_App bnds=[40,833][200,1033]} from pid 871
I/my_app( 6915): onCreate
I/my_app( 6915): onResume
#tape the home button
I/my_app( 6915): onPause
#restart from notification in launcher bar
I/ActivityManager( 504): START u0 {flg=0x34400000 cmp=com.domain.my_app/.My_App bnds=[0,102][720,230]} from pid -1
I/PSI Recorder( 6915): onResume
#it is necessary to use two times the my_app quit button in order to exit the application, and then in log, I noted the following error
E/StrictMode( 6915): class com.domain.my_app; instances=2; limit=1
E/StrictMode( 6915): android.os.StrictMode$InstanceCountViolation: class com.domain.my_app; instances=2; limit=1
アプリケーション、リモート サービス、通知に関する多くのこと、ヒントなどを読みましたが、アプリケーションがリストから再起動されたときに UI が再作成される理由を理解できず、以前に通知から再表示された場合にのみ...注: FLAG_ACTIVITY_NEW_TASK フラグがインテントに追加されていない場合、次の警告がログに記録され、観察された動作は変更されません...
W/ActivityManager( 504): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { flg=0x24400000 cmp=com.domain.my_app/.My_App bnds=[0,102][720,230] }
どんな助けにも感謝します。
ありがとうございました。よろしくお願いします、
編集:「アプリケーション」マニフェストフィールドの代わりに、android:launchMode="singleTop" 属性が「アクティビティ」要素にある必要があります:/
動いたけど動いてる
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:allowBackup="true">
<activity
android:name=".my_app"
android:configChanges="orientation"
android:label="@string/app_name"
android:launchMode="singleTop"