【发布时间】:2018-05-25 08:54:50
【问题描述】:
我正在使用 Android Studio。
我为接收 android.intent.action.BOOT_COMPLETED 的应用添加了一个 BroadcastReceiver,接收器只是显示一个 Toast 进行测试。问题是我在 Android 启动后收到“应用程序已停止”消息。
我的第一个问题是:有没有办法在启动时调试它并自己查看问题出在哪里?因为我在 Android Studio 中看不到任何涉及该问题的日志。
我的第二个问题与问题本身有关。这是代码: XML:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="myapp">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".AutoStartReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
</application>
</manifest>
Java:广播接收器
public class AutoStartReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "loaded", Toast.LENGTH_LONG).show();
throw new UnsupportedOperationException("Not yet implemented");
}
}
第三个问题是:在receiver中做一些繁重的工作(读取一些文件并设置AlarmManager)而不是创建一个服务是否可以?因为从 API 26 开始,Android 对服务施加了很多限制。
谢谢
【问题讨论】:
标签: java android broadcastreceiver boot