【问题标题】:Application crashing after boot启动后应用程序崩溃
【发布时间】:2014-08-13 21:19:08
【问题描述】:

我正在创建一个需要在启动时启动的 android 应用程序,我做了一切必要的事情来实现这一点,但是当设备启动时它就崩溃了。如果我在之后手动打开应用程序,它会正常打开:它只会在设备刚刚启动时崩溃,而不是在用户手动打开它时崩溃。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:installLocation="internalOnly"
package="com.example.remind" >

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".MainMenu"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver
        android:name=".AlarmGenerator"
        android:enabled="true"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.example.remind.STOP" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.example.remind.START" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

AlarmGenerator.java

public class AlarmGenerator extends BroadcastReceiver {

private AlarmManager alarm;
private PendingIntent send;

public AlarmGenerator() {
}

@Override
public void onReceive(Context context, Intent intent) {

    alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, Reminder.class);
    send = PendingIntent.getActivity(context, 0, i, 0);

    if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
        Intent start = new Intent(context, MainMenu.class);
        start.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(new Intent(context, MainMenu.class));
    }else if(intent.getAction().equals("com.example.remind.STOP")){
        alarm.cancel(send);
    }else {
        start();
    }
}

private void start(){
    alarm.setRepeating(AlarmManager.RTC, System.currentTimeMillis() + ((1000 * 30) * 1), (30 * 1000) * 1, send);
    }
}

我搜索了其他答案,它们是关于拼写错误的课程之类的,但这似乎无缘无故地崩溃了(但必须有一个)。 IDK 如果重要,但我的目标是 API 16,最低 SDK 版本也是 16,在另一个项目中我做了类似的事情,但我启动了一项服务而不是 Activity,但最低 SDK 版本是 7(姜饼)和目标 16(果冻豆)。

【问题讨论】:

  • 当你说崩溃时,是 ANR 还是强制关闭?
  • 发布您的 logcat 内容
  • 它只是显示一条消息说“提醒已停止工作”我的手机是西班牙语,所以我不知道确切的英语消息。
  • 尝试缩小问题范围。在这里和那里删除一些代码,看看问题是否仍然存在。正如@TonyChhabada 所说,发布相关的logcat。
  • 重启时设备断开连接如何获取logcat?

标签: java android broadcastreceiver boot


【解决方案1】:

您可能在 logcat 中看到如下;

“从 Activity 上下文之外调用 startActivity() 需要 FLAG_ACTIVITY_NEW_TASK 标志。这真的是你想要的吗?”

所以,替换你的代码如下。

if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
    Intent start = new Intent(context, MainMenu.class);
    start.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        context.startActivity(new Intent(context, MainMenu.class));
+        context.startActivity(start);
    }else if(intent.getAction().equals("com.example.remind.STOP")){

【讨论】:

  • 嗯,我已经这样做了。它在我展示的代码中。我之前看到了一个例子,所以我添加了它,但它仍然不起作用。
  • 为什么在调用 startActivity() 时创建新的 Intent 实例,而不是你已经创建的“start”?如果在 Activity 之外调用 startActivity(),您应该在意图中设置 FLAG_ACTIVITY_NEW_TASK 标志,但在您的代码中,您不使用设置标志的意图。
  • 成功了!我没有注意那个。非常感谢。
【解决方案2】:

确保在加载应用程序之前加载所需的任何先决条件(您确实无法获得不需要的先决条件;))。例如,如果它需要 JRE,因为它是 Android。

【讨论】:

  • 嗯,我该如何检查?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
相关资源
最近更新 更多