如果用户从首选项中选择了某些东西,我需要设置 AlaramManager。但我得到了 不幸的是,停止了 Error ,你认为我的问题在哪里?谢谢
接收者 :
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