0

如果用户从首选项中选择了某些东西,我需要设置 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
4

2 回答 2

0

使用 LogCat 检查与“停止的错误”相关的 Java 堆栈跟踪。

在这种情况下,问题可能是您有 android:permission="android.permission.RECEIVE_BOOT_COMPLETED",这是不正确的。它要求发送该广播的人必须具有该权限,这不一定是正确的。

于 2013-08-05T14:10:43.400 回答
0

我注意到的一件事是您忘记了其中的点,android:name="AutoSender"所以它应该是android:name=".AutoSender"

此外,当您的 BroadcastReceiver 不在主包中时(不知道是否在您的情况下),您应该像这样将包名称放在它前面android:name="my.package.name.AutoSender"

于 2013-08-05T14:26:28.150 回答