我有一个测试应用,可以使用其类名启动内部设置活动:
Intent intent = new Intent(this, MyTestAppSettings.class);
startActivity(intent);
在活动出现之前有一个很长的延迟(此特定示例为10秒),延迟是由于ActivityManager启动超时到期。我添加了一些日志语句来说明这种情况,见下文。
对 settingsClick 的调用发生在主活动中,您会看到ActivityManager启动intent。然后在为启动的活动输入 onCreate 之前有10秒的延迟。在此期间,ActivityManager记录了启动超时已过期消息。
导致此类超时的原因是什么?
05-15 19:46:25.673: I/MyTestApp(9341): settingsClick: Launching MyTestAppSettings activity
05-15 19:46:25.673: I/ActivityManager(1672): Starting: Intent { cmp=com.MyCompany.MyTestApp/.MyTestAppSettingsActivity } from pid 9341
05-15 19:46:26.193: W/ActivityManager(1672): Activity pause timeout for HistoryRecord{4055c610 com.MyCompany.MyTestApp/.MyTestAppActivity}
05-15 19:46:31.683: I/power(1672): *** set_screen_state 1
05-15 19:46:35.693: W/ActivityManager(1672): Launch timeout has expired, giving up wake lock!
05-15 19:46:35.903: I/MyTestAppSettings(9341): onCreate Enter
05-15 19:46:35.923: I/MyTestAppSettings(9341): onCreate Exit
05-15 19:46:35.923: I/MyTestAppSettings(9341): onResume Enter
05-15 19:46:35.923: I/MyTestAppSettings(9341): onResume Exit
05-15 19:46:36.043: I/ActivityManager(1672): Displayed com.MyCompany.MyTestApp/.MyTestAppSettingsActivity: +9s850ms
编辑以下是设置活动的代码,目前非常薄,仅测试主应用中的活动启动。
@SuppressWarnings("unused")
public class MyTestAppSettingsActivity extends Activity {
protected static final String TAG = "MyTestAppSettings"; // for Log.x() calls
public static final boolean DEBUG_LOGGING = false; // verbose logging using Log.i()
public static final boolean DEBUG_PROCS = true; // enables proc entry/exit logging
private Context mThisActivityContext = null;
private Resources mResources = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
if (DEBUG_PROCS) { Log.i(TAG, "onCreate Enter"); }
super.onCreate(savedInstanceState);
setContentView(R.layout.monitor_settings);
mThisActivityContext = this.getApplicationContext();
mResources = mThisActivityContext.getResources();
if (DEBUG_PROCS) { Log.i(TAG, "onCreate Exit"); }
}
@Override
protected void onResume()
{
if (DEBUG_PROCS) { Log.i(TAG, "onResume Enter"); }
super.onResume();
if (DEBUG_PROCS) { Log.i(TAG, "onResume Exit"); }
}
@Override
protected void onPause()
{
if (DEBUG_PROCS) { Log.i(TAG, "onPause Enter"); }
super.onPause();
if (DEBUG_PROCS) { Log.i(TAG, "onPause Exit"); }
}
@Override
public void onStop() {
if (DEBUG_PROCS) { Log.i(TAG, "onStop Enter"); }
super.onStop();
if (DEBUG_PROCS) { Log.i(TAG, "onStop Exit"); }
}
}
设置活动布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:paddingLeft="3dip"
android:text="@string/displaySystemSettingsHeader"
style="@style/SettingsHeader"
/>
<LinearLayout
android:id="@+id/layoutSettings"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/imageButtonSettings"
android:layout_width="36dip"
android:layout_height="36dip"
android:clickable="true"
android:contentDescription="@string/btnSettings"
android:scaleType="fitStart"
android:src="@drawable/button_settings"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="3dip"
android:text="@string/displaySystemSettings"
style="@style/SettingsEntryTitle"
/>
</LinearLayout>
<CheckBox
android:id="@+id/chkInfoMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
style="@style/SettingsEntryTitle"
android:text="@string/infoButtonMode" />
</LinearLayout>