如果用户从偏好中选择了某些内容,我需要设置AlaramManager。但我很遗憾,停止错误,你认为我的问题在哪里?谢谢
接收者:
public class StartMyServiceAtBootReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(context);
if (prefs.getString("autoSend", null) != null) {
if (prefs.getString("autoSend", "").equals("true")) {
AlarmManager am = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, AutoSender.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP,
System.currentTimeMillis(), 1000 * 10, pi);
}
}
}
}
}
意图:
@SuppressLint("Wakelock")
public class AutoSender extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent arg1) {
PowerManager pm = (PowerManager) context
.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK, "");
wl.acquire();
Toast.makeText(context, "Test" , Toast.LENGTH_LONG)
.show();
wl.release();
}
}
清单:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
------------------
<receiver android:name="AutoSender" >
</receiver>
<receiver
android:name=".StartMyServiceAtBootReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
编辑:
以下是例外:
08-07 11:21:46.825: E/AndroidRuntime(668): FATAL EXCEPTION: main
08-07 11:21:46.825: E/AndroidRuntime(668): java.lang.RuntimeException: Unable to start receiver com.aps.safirsms.StartMyServiceAtBootReceiver: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
08-07 11:21:46.825: E/AndroidRuntime(668): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2383)
08-07 11:21:46.825: E/AndroidRuntime(668): at android.app.ActivityThread.access$1500(ActivityThread.java:141)
08-07 11:21:46.825: E/AndroidRuntime(668): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1310)
08-07 11:21:46.825: E/AndroidRuntime(668): at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 11:21:46.825: E/AndroidRuntime(668): at android.os.Looper.loop(Looper.java:137)
08-07 11:21:46.825: E/AndroidRuntime(668): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-07 11:21:46.825: E/AndroidRuntime(668): at java.lang.reflect.Method.invokeNative(Native Method)
08-07 11:21:46.825: E/AndroidRuntime(668): at java.lang.reflect.Method.invoke(Method.java:511)
08-07 11:21:46.825: E/AndroidRuntime(668): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-07 11:21:46.825: E/AndroidRuntime(668): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-07 11:21:46.825: E/AndroidRuntime(668): at dalvik.system.NativeStart.main(Native Method)
08-07 11:21:46.825: E/AndroidRuntime(668): Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
08-07 11:21:46.825: E/AndroidRuntime(668): at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:224)
08-07 11:21:46.825: E/AndroidRuntime(668): at com.aps.safirsms.StartMyServiceAtBootReceiver.onReceive(StartMyServiceAtBootReceiver.java:18)
08-07 11:21:46.825: E/AndroidRuntime(668): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2376)
08-07 11:21:46.825: E/AndroidRuntime(668): ... 10 more
答案 0 :(得分:0)
使用LogCat检查与“已停止的错误”关联的Java堆栈跟踪。
在这种情况下,问题可能是您android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
,这是不正确的。它要求发送该广播的任何人都拥有该权限,这不一定是真的。
答案 1 :(得分:0)
我注意到的一件事是你忘了android:name="AutoSender"
中的点,所以它应该是android:name=".AutoSender"
。
此外,当您的BroadcastReceiver不在主包中时(不知道是否在您的情况下),您应该将包名称放在它前面,如android:name="my.package.name.AutoSender"