我正在創建在狀態欄中顯示通知圖標的應用程序。當用戶打開狀態欄並點擊圖標時,應用程序應該啓動。從狀態欄啓動應用程序而無需重新創建
我正在尋找一種避免在此次啓動過程中重新創建應用程序的方法。我已經創建了測試應用程序,並向處理程序onCreate,onRestart,onResume,onStop和onDestroy添加日誌消息。日誌消息說明了問題:
- 用戶啓動應用程序 - 的onCreate,的onResume
- 用戶按下HOME鍵 - 的onStop
- 用戶打開的應用程序列表並重新開始應用 - onRestart,的onResume
- 用戶按下HOME按鈕 - onStop
- 用戶打開最近的應用程序列表並選擇應用程序 - onRestart,onResume
- 用戶按HOME按鈕 - onStop
- 用戶打開狀態欄並點擊應用程序圖標 - onDestroy,onCreate,onResume。
第7步有一個不同的行爲,然後3)和5) - 這裏有onDestroy。換句話說,應用程序的實例被重新創建。是否有可能避免這種娛樂?
這是我的測試活動的代碼:
public class MainActivity extends Activity {
private final String LOG_TAG = "com.example.notificationtest";
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showNotification(this);
Log.d(LOG_TAG, "NotificationTest: OnCreate");
}
@Override protected void onRestart() {
super.onRestart();
Log.d(LOG_TAG, "NotificationTest: OnRestart");
}
@Override protected void onResume() {
super.onResume();
Log.d(LOG_TAG, "NotificationTest: OnResume");
}
@Override protected void onDestroy() {
super.onDestroy();
Log.d(LOG_TAG, "NotificationTest: OnDestroy");
}
@Override protected void onStop() {
super.onStop();
Log.d(LOG_TAG, "NotificationTest: OnStop");
}
private static final int NOTIF_ID = 91371;
public static void showNotification(Context context) {
final Intent result_intent = new Intent(context, MainActivity.class);
result_intent.setAction(Intent.ACTION_MAIN);
result_intent.addCategory(Intent.CATEGORY_LAUNCHER);
//result_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//result_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//result_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder stack_builder = TaskStackBuilder.create(context);
stack_builder.addParentStack(MainActivity.class);
stack_builder.addNextIntent(result_intent);
PendingIntent pending_intent = stack_builder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
android.support.v4.app.NotificationCompat.Builder builder = new android.support.v4.app.NotificationCompat.Builder(context);
Resources res = context.getResources();
builder.setContentIntent(pending_intent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker("test")
.setWhen(System.currentTimeMillis())
.setAutoCancel(false)
.setContentTitle("title")
.setContentInfo("cinfo")
.setContentText("ctext");
Notification n = builder.build();
n.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(NOTIF_ID, n);
}
}
有一些標誌,可以設置爲result_intent,像FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_CLEAR_TASK和FLAG_ACTIVITY_NEW_TASK。它們允許指定在啓動時應重新啓動活動(使用活動啓動模式「singleTop」,「singleTask」等)。但是應該設置什麼標誌以避免重新啓動?可能我應該以某種方式配置pending_intent?
任何幫助將不勝感激。
解決
非常感謝您的答案,問題就解決了。
同樣的問題被描述爲here。我檢查了該主題的測試項目,發現與我的代碼有差異。爲了解決這個問題,我的代碼應該如下方法改變:
final Intent result_intent = new Intent(context, MainActivity.class);
//result_intent.setAction(Intent.ACTION_MAIN); // (1)
//result_intent.addCategory(Intent.CATEGORY_LAUNCHER); // (2)
result_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
另一組標誌工程太:
result_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
主要觀點是註釋行(1)和(2)
是的onSaveInstanceState'()'和'onRestoreInstance()'被稱爲?比嘗試解決活動生命週期更好的選擇可能是通過這些方法來恢復基本運行數據,這會使您存儲的數據在onDestroy中保持不變。 – 2013-03-05 06:48:57
看看這個SO帖子:http://stackoverflow.com/questions/3305088/how-to-make-notification-intent-resume-rather-than-making-a-new-intent – appsroxcom 2013-03-05 07:00:01
是的,onSaveInstanceState()和onRestoreInstance () 叫做。我不'嘗試解決生命週期。這裏描述了問題的根源:http://forum.unity3d.com/threads/172700-open-Unity-application-using-notification-in-statusbar?p=1181292#post1181292 – dvpublic 2013-03-05 09:59:49