OnBootReceiver可以工作,但找不到AlarmReceiver

时间:2012-02-03 08:48:05

标签: java android cordova mobile

我正在尝试在启动后立即设置警报接收器。因此,我有一个应该注册警报的OnBootReceiver。 onBootReceiver工作,它被调用,但不知何故它找不到我的AlarmReceiver类。

OnBootReceiver在启动后成功启动:

public class OnBootReceiver extends BroadcastReceiver {

    private static final String TAG = "OnBootReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "called");

        Intent i = new Intent(context, com.packagenames.AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getService(context, 0, i, 0);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Calendar time = Calendar.getInstance();
        time.setTimeInMillis(System.currentTimeMillis());
        time.add(Calendar.SECOND, 30);

        am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), time.getTimeInMillis(), pi);

    }

}

正如您所看到的,它配置警报尝试调用com.packagenames.AlarmReceiver.class。该类存在且位于同一个包中:

public class AlarmReceiver extends BroadcastReceiver {

    private static final String TAG = "AlarmReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "alarm received");
        Intent i = new Intent(context, com.packagename.DataService.class);
        i.putExtra("action", "process");
        context.startService(i);
    }

}

不幸的是,我收到以下错误:

  

02-03 09:22:25.344:W / ActivityManager(103):无法启动服务   意图{flg = 0x4 cmp = com.phonegap.packagename / .AlarmReceiver(有附加内容)   }:未找到

Android Manifest看起来像这样

<application>

// activities etc

        <receiver
            android:name="com.phonegap.packagename.OnBootReceiver"
            android:enabled="true"
            android:exported="false"
            android:label="OnBootReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <receiver
            android:name="com.phonegap.packagename.AlarmReceiver"
            android:enabled="true"
            android:label="AlarmReceiver">
            <intent-filter>
            </intent-filter>
        </receiver>

    </application>
你知道错了吗?也许我忘记了什么?

由于

编辑:在清单中,我添加了

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

为了使OnBootReceiver工作。警报需要类似的东西吗?

2 个答案:

答案 0 :(得分:1)

创建待定意图时,不应该使用getBroadcast而不是getService吗?

答案 1 :(得分:0)

如果你的应用程序没有安装在SD卡上,整个接收器的东西才有效。将其添加到清单文件中:

机器人:的installLocation = “internalOnly”