猴子在子活动中启动意图会导致 ANR

发布于 2024-11-10 15:15:43 字数 2510 浏览 7 评论 0原文

我有一个 Android 应用程序,其主 Activity 上有一个按钮,用于创建第二个 PreferenceActivity 来显示设置。

清单看起来像这样

<application android:name=".MyApp" 
  android:icon="@drawable/icon" 
  android:label="@string/app_name">
  <activity android:name=".MyApp"
    android:label="@string/app_name"
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity android:name=".Settings"></activity>
</application>

当我使用 Monkey 进行测试时出现问题。它将启动我的应用程序,然后按下按钮创建我的 PreferenceActivity。创建 PreferenceActivity 后,monkey 将发送启动另一个包的意图。 PreferenceActivity 将暂停,其他包运行。然后猴子发送另一个意图来启动我原来的应用程序。我看到我的 PreferenceActivity 进入了 onResume(),然后它冻结并发生 ANR。

我在 logcat 中看到:

INFO/ActivityManager(211): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.MyApp/.MyApp } from pid 4532
# Main activity is created and runs.
# Monkey presses settings button, which sends intent to start PreferenceActivity.
INFO/ActivityManager(211): Starting: Intent { cmp=com.example.MyApp/.Settings } from pid 4519
# PreferenceActivity is created and runs.
# Monkey sends intent to start other package.  PreferenceActivity pauses.
INFO/ActivityManager(211): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.OtherApp/.OtherApp } from pid 4532
# Other app runs.
# Monkey sends intent to start MyApp again.
INFO/ActivityManager(211): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.MyApp/.MyApp } from pid 4532
# PreferenceActivity's onResume() method runs.
# Screen is blank.
ERROR/ActivityManager(211): ANR in com.example.MyApp (com.example.MyApp/.Settings)

我删除了 PreferencesActivity 中的所有内容,所以剩下的就是

public class Settings extends PreferenceActivity 
{
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
    addPreferencesFromResource(R.xml.preferences);
  }
}

恢复 PreferenceActivity 时我仍然收到 ANR。但是,如果我将其更改为扩展 Activity 而不是 PreferenceActivity 并删除 onCreate() 中的首选项内容,我将不再收到 ANR。如果我尝试使用“adb shell am”手动启动意图,我也不会收到 ANR,因此我的问题仅在运行 Monkey 时出现。

任何帮助将不胜感激。 谢谢。

I have an android app with a button on the main activity that creates a second PreferenceActivity to display settings.

The manifest looks like this

<application android:name=".MyApp" 
  android:icon="@drawable/icon" 
  android:label="@string/app_name">
  <activity android:name=".MyApp"
    android:label="@string/app_name"
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />
      <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity android:name=".Settings"></activity>
</application>

The problem arises when I was using Monkey to test. It will start my app, then press the button to create my PreferenceActivity. Once the PreferenceActivity is created, monkey will send an intent to start another package. The PreferenceActivity will pause and the other package runs. Then monkey sends another intent to start my original app. I see my PreferenceActivity enter the onResume() and then it freezes and ANR occurs.

I see in my logcat:

INFO/ActivityManager(211): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.MyApp/.MyApp } from pid 4532
# Main activity is created and runs.
# Monkey presses settings button, which sends intent to start PreferenceActivity.
INFO/ActivityManager(211): Starting: Intent { cmp=com.example.MyApp/.Settings } from pid 4519
# PreferenceActivity is created and runs.
# Monkey sends intent to start other package.  PreferenceActivity pauses.
INFO/ActivityManager(211): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.OtherApp/.OtherApp } from pid 4532
# Other app runs.
# Monkey sends intent to start MyApp again.
INFO/ActivityManager(211): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.MyApp/.MyApp } from pid 4532
# PreferenceActivity's onResume() method runs.
# Screen is blank.
ERROR/ActivityManager(211): ANR in com.example.MyApp (com.example.MyApp/.Settings)

I removed everything in my PreferencesActivity so all it has left is

public class Settings extends PreferenceActivity 
{
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
    addPreferencesFromResource(R.xml.preferences);
  }
}

I still get the ANR when resuming the PreferenceActivity. BUT, if I change it to extend Activity instead of PreferenceActivity and remove the preference things in onCreate(), I don't get the ANR anymore. I also don't get an ANR if I try to start intents manually with "adb shell am", so my problem only occurs when I run Monkey.

Any help would be most appreciated.
Thank you.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文