【发布时间】:2011-08-16 20:08:03
【问题描述】:
我正在构建的 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
}
}
Manifest 文件内容:
<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 方法总是在应用程序启动时被调用。从早上开始,我已经尽我所能尝试了,但没有任何效果,无法查明问题所在,如果你们中的任何人能对此有所了解,那将不胜感激。
谢谢
【问题讨论】:
标签: android homescreen