我在构建的 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
}
}
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 Intent 来初始化该部分,但这将需要一些代码重构,除非有解决方案,否则我目前不愿意这样做。
所以我很想知道这是否是标准行为,或者是否存在导致这种情况的已知错误,因为我的假设是应用程序的 onCreate
方法将始终在以下情况下被调用:应用程序已启动。从早上开始我已经尽了一切努力,但没有任何效果,无法查明问题所在,如果你们中的任何人能够对此有所了解,那么我们将不胜感激。
谢谢
最佳答案
好吧,我终于找到了问题所在,它出在我的代码本身中。我最初怀疑MyApplication
的onCreate()
方法没有被调用,我有这样的假设,因为我看不到任何日志。要知道该方法是否被调用,而不是使用 Log.i()
我还在 ArrayList 中附加了一些额外的日志消息并稍后打印它们,这表明这些方法确实被调用,甚至服务已正确实例化,但数据或文件列表未填充,因为当时 SDCard 尚未准备好。我也非常确定日志在 Logcat 上不可用,因为 USB 调试器在我的应用程序启动后准备就绪(因为它是主屏幕应用程序)。
当您看到我重写的 MyApplication
时,实际问题就变得显而易见了。类实现一个名为 ExternalStorageListener
的监听器它基本上扩展了 BroadcastReceiver,我创建了该类来接收 SDCard 相关 Intent,例如 ACTION_MEDIA_MOUNTED
, ACTION_MEDIA_REMOVED
重建数据(文件列表)。就我而言,ExternalStorageListener 类没有接收 Intent ,因为我忘记添加此 filter.addDataScheme("file")
在 registerExternalStorageObserver
代码示例中的方法。
我确实同意我的问题是基于错误的假设,并且我发布的代码示例很难弄清楚实际问题。我不确定如何处理这个问题,是将其标记为答案还是保持原样。
关于重启期间主屏幕启动器应用程序不会调用 Android 应用程序 onCreate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7084337/