我在構建Android應用程序時遇到了一個奇怪的問題,該應用程序基本上是主屏幕替換應用程序,它將作爲默認主屏幕放在設備中。做一些初始化的工作,我已經延長Android應用程序類,並在onCreate()
方法我基本上是註冊一些觀察者和啓動服務,下面的代碼:Android應用程序onCreate在重新啓動期間未調用主屏幕啓動器應用程序
<application android:persistent="true" android:icon="@drawable/icon"
android:label="@string/app_name" android:name="com.webgyani.android.MyApplication"
android:debuggable="true">
<activity android:name=".HomeTabActivity" android:launchMode="singleInstance"
android:stateNotNeeded="true" android:theme="@style/LightTabsTheme"
android:screenOrientation="landscape" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
:清單文件的
public class MyApplication extends Application implements ExternalStorageListener {
private ExternalStorageObserver externalStorageObserver;
public void onCreate() {
Log.i("MyApplication", "Starting application");
super.onCreate();
externalStorageObserver = new ExternalStorageObserver(this);
if(externalStorageObserver.isExternalStorageAvailable()) {
// this builds a list of files present in the SD card
// which is being used through the application to do
// some work
buildData();
File externalFileDir = getApplicationContext().getExternalFilesDir(null);
if(externalFileDir != null && externalFileDir.isDirectory()) {
// do something...
}
}
//Register listener to observe external storage state
registerExternalStorageObserver();
Log.i("SyncService", "Starting sync service...");
ComponentName cmp = startService(new Intent(getApplicationContext(), SyncService.class));
Log.i("SyncService", cmp.toString());
}
private void registerExternalStorageObserver() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_REMOVED);
registerReceiver(externalStorageObserver, filter);
}
private void buildData() {
// builds a list
}
}
內容當我使用Eclipse安裝應用程序或手動將apk安裝到設備上時,此工作正常。一旦應用程序安裝的東西工作正常,我的意思是上述onCreate()
方法被調用,服務也正常啓動,但如果我重新啓動設備,這次onCreate()
方法不會被調用(沒有出現任何日誌語句,也沒有服務沒有開始)。經過一些調試後,我發現只有當我將應用程序設置爲默認啓動程序/主屏幕應用程序並重啓設備時,纔會發生這種情況,因爲一旦將應用程序設置爲默認啓動程序,Android應該會在重新啓動後自動啓動您的應用程序作爲主屏幕。在我的情況下,應用程序啓動,但代碼不執行。
我試圖使用調試器,但沒有工作,因爲當我重新啓動設備調試器斷開連接,並通過USB調試啓用時,我的應用程序已啓動。
我甚至雙重檢查了Logcat,但沒有看到任何錯誤。我想有一個BOOT_COMPLETED的意圖來初始化那個部分,但是這需要一些代碼重構,除非有解決方案,否則我不願意在這個時間點進行重構。
所以我很好奇知道這是否是標準行爲,或者是否存在導致此問題的已知錯誤,因爲我的假設是應用程序的onCreate
方法將始終在應用程序啓動時調用。自上午以來,我盡我所能,但沒有任何工作,無法確定問題,如果你們中的任何一位能夠對此有所瞭解,那麼我將非常感激。
感謝