重启期间主屏幕启动器应用程序不会调用 Android 应用程序 onCreate

发布于 2024-11-30 01:47:51 字数 3107 浏览 1 评论 0原文

我在构建的 Android 应用程序中遇到了一个奇怪的问题,该应用程序基本上是一个主屏幕替换应用程序,它将作为设备中的默认主屏幕。为了进行一些初始化工作,我扩展了 Android Application 类,在 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() code> 方法没有被调用(没有日志语句出现,服务也没有启动)。经过一些调试后,我注意到只有当我将应用程序设置为默认启动器/主屏幕应用程序并随后重新启动设备时才会发生这种情况,因为一旦将应用程序设置为默认启动器,Android 应该在重新启动后自动将应用程序作为主屏幕启动。就我而言,应用程序已启动,但代码未执行。

我尝试使用调试器,但这不起作用,因为当我重新启动设备时,调试器会断开连接,并且当启用 USB 调试时,我的应用程序已经启动。

我什至仔细检查了 Logcat 但没有看到任何错误。我想过用 BOOT_COMPLETED 意图来初始化该部分,但这将需要一些代码重构,除非有解决方案,否则我目前不愿意这样做。

所以我很想知道这是否是标准行为,或者是否存在导致此问题的已知错误,因为我的假设是应用程序的 onCreate 方法将在应用程序启动时始终被调用。从早上开始我已经尽了一切努力,但没有任何效果,无法查明问题所在,如果你们中的任何人能够对此有所了解,那么我们将不胜感激。

谢谢

I am having a strange problem in an Android application that I am building, the application is basically a Homescreen replacement app which will be put as a default homescreen in a device. To do some initialization work I have extended Android Application class and in the onCreate() method I am basically registering some observer and starting a service, here's the code:

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

Content of Manifest file:

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

This works fine when I install the app either using Eclipse or manually installing the apk onto the device. Once the app is installed things work fine, I mean the above onCreate() method gets called and service is also started normally, but if I reboot the device this time the onCreate() method does not get called(none of the log statements appear and also the services are not started). After some debugging I noticed that this only happens if I set my app as the default launcher/homescreen app and thereafter reboots the device, because once you set the app as the default launcher, Android should automatically launch your app as the homescreen after reboot. In my case the app is launched but that code is not executed.

I tried to use debugger but that didn't work because when I reboot the device the debugger gets disconnected and by the time USB debugging gets enabled my app is already started.

I even double checked the Logcat but didn't see any error. I thought of having a BOOT_COMPLETED intent to initialize that part, but that will require some code refactoring which I am not willing to do at this point of time unless there is a solution.

So I am curious to know that whether this is a standard behavior, or is there a known bug which causes this, because my assumption is the onCreate method of the Application will always get called whenever the app is started. I have tried whatever I could since morning, but nothing worked, couldn't pinpoint the issue, if any of you could shed some light into this then that would be highly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

茶底世界 2024-12-07 01:47:51

好吧,终于我找到了问题所在,而且问题出在我的代码本身中。我最初怀疑 MyApplicationonCreate() 方法没有被调用,我有这个假设是因为我看不到任何日志。为了知道该方法是否被调用,我没有使用 Log.i(),而是在 ArrayList 中附加了一些额外的日志消息并稍后打印它们,这表明这些方法确实正在被调用调用,甚至服务已正确实例化,但数据或文件列表未填充,因为当时 SDCard 尚未准备好。我也非常确定日志在 Logcat 上不可用,因为 USB 调试器在我的应用程序启动后准备就绪(因为它是主屏幕应用程序)。

当您看到我重写的 MyApplication 类实现了一个名为ExternalStorageListener 的侦听器(它基本上扩展了 BroadcastReceiver)时,实际问题变得显而易见,我创建了该类来接收与 SDCard 相关的 Intents 例如 < code>ACTION_MEDIA_MOUNTED, ACTION_MEDIA_REMOVED 重建数据(文件列表)。就我而言,ExternalStorageListener 类未接收 Intents,因为我忘记在代码示例中的 registerExternalStorageObserver 方法中添加此 filter.addDataScheme("file")

我确实同意我的问题是基于错误的假设,并且我发布的代码示例很难找出实际问题。我不确定如何处理这个问题,是将其标记为答案还是保持原样。

Well, finally I figured out the problem, and it was in my code itself. I initially suspected that the MyApplication's onCreate() method was not getting called, I had that assumption because I was not able to see any logs. To know whether the method is getting called or not, instead of using Log.i() I was also appending some additional log messages in an ArrayList and printing them later, this revealed that the methods were indeed getting called and even the Service was instantiating properly but the data or filelist is not being populated because the SDCard was not ready by that time. I am also pretty sure that the logs were not available on Logcat due to the fact that the USB debugger becomes ready after my app is started(as it's a homescreen app).

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). In my case the ExternalStorageListener class was not receiving the Intents because I forgot to add this filter.addDataScheme("file") in the registerExternalStorageObserver method above in the code sample.

I do agree that my question was based on a false assumption and the code example I posted it's kind of hard to figure out the actual issue. I am not sure what to do with the question whether to mark this as answer or leave it as it is.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文