簡體   English   中英

Android 應用程序 onCreate 在重新啟動期間未調用主屏幕啟動器應用程序

[英]Android Application onCreate not called for homescreen launcher app during reboot

我正在構建的 Android 應用程序中遇到一個奇怪的問題,該應用程序基本上是一個主屏幕替換應用程序,它將作為設備中的默認主屏幕放置。 為了做一些初始化工作,我擴展了 Android 應用程序 class 並在onCreate()方法中我基本上注冊了一些觀察者並啟動了服務,這里是代碼:

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
    }   
}

清單文件內容:

<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>

當我使用 Eclipse 或手動將 apk 安裝到設備上安裝應用程序時,這工作正常。 安裝應用程序后,一切正常,我的意思是上面的onCreate()方法被調用並且服務也正常啟動,但是如果我這次重新啟動設備, onCreate()方法不會被調用(沒有任何日志語句出現並且服務也沒有啟動)。 經過一些調試,我注意到只有當我將我的應用程序設置為默認啟動器/主屏幕應用程序然后重新啟動設備時才會發生這種情況,因為一旦您將應用程序設置為默認啟動器,Android 應該會在重新啟動后自動啟動您的應用程序作為主屏幕。 在我的情況下,應用程序已啟動,但未執行該代碼。

我嘗試使用調試器,但這不起作用,因為當我重新啟動設備時,調試器斷開連接,並且當 USB 調試啟用時,我的應用程序已經啟動。

我什至仔細檢查了 Logcat,但沒有看到任何錯誤。 我想有一個 BOOT_COMPLETED 意圖來初始化該部分,但這將需要一些代碼重構,除非有解決方案,否則我目前不願意這樣做。

所以我很想知道這是否是標准行為,或者是否存在導致這種情況的已知錯誤,因為我的假設是應用程序的onCreate方法總是會在應用程序啟動時被調用。 從早上開始,我已經盡我所能嘗試了,但沒有任何效果,無法查明問題所在,如果你們中的任何人都可以對此有所了解,那將不勝感激。

謝謝

好吧,最后我找到了問題所在,它就在我的代碼中。 我最初懷疑MyApplicationonCreate()方法沒有被調用,我有這個假設是因為我看不到任何日志。 要知道該方法是否被調用,而不是使用Log.i() ,我還在 ArrayList 中附加了一些額外的日志消息並稍后打印它們,這表明這些方法確實被調用了,甚至服務正在實例化正確,但未填充數據或文件列表,因為那時 SDCard 尚未准備好。 我也很確定日志在 Logcat 上不可用,因為 USB 調試器在我的應用程序啟動后准備就緒(因為它是一個主屏幕應用程序)。

The actual problem becomes obvious when you see that my overridden MyApplication class implements a listener called ExternalStorageListener which basically extends BroadcastReceiver, I have created that class to receive SDCard related Intents for example ACTION_MEDIA_MOUNTED , ACTION_MEDIA_REMOVED to rebuild the data(file list). 在我的例子中,ExternalStorageListener class 沒有收到 Intent,因為我忘記在上面的代碼示例中的registerExternalStorageObserver方法中添加這個filter.addDataScheme("file")

我同意我的問題是基於錯誤的假設,而我發布的代碼示例很難弄清楚實際問題。 我不確定如何處理這個問題是將其標記為答案還是保持原樣。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM