警报服务无法正常工作

时间:2014-10-07 07:54:53

标签: android broadcastreceiver android-alarms

在我的应用程序中,我需要每隔午夜运行一些代码。这是我第一次开始报警服务,并在网上搜索了一些片段和示例。

下面是我的代码,它没有按预期工作,请帮助我指出我的错误:

设置重复警报

private static void initiateMidnightAlarm(Context context, AlarmManager alarmManager) {
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 0);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    PendingIntent midnightIntent = PendingIntent.getService(context, 0,
            new Intent("com.alarms.MidnightProcess"), 0);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
            AlarmManager.INTERVAL_DAY, midnightIntent);
}

在清单中宣布收件人:

<receiver
    android:name=".receivers.MidnightProcessReceiver"
    android:enabled="true"
    android:exported="false">
    <intent-filter>
        <action android:name="com.alarms.MidnightProcess"/>
    </intent-filter>
</receiver>

RECEIVER CODE:

public class MidnightProcessReceiver extends BroadcastReceiver {
    public MidnightProcessReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        processMidnightLogic(context);
    }

    public static void processMidnightLogic(Context context) {
        Toast.makeText(context,"MidnightProcessReceiver",Toast.LENGTH_LONG).show();
    }
}

更新

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.appname">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    <uses-permission android:name="android.permission.CALL_PHONE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.GET_ACCOUNTS"/>
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
    <uses-permission android:name="android.permission.GET_TASKS"/>
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <permission
        android:name="com.appname.permission.C2D_MESSAGE"
        android:protectionLevel="signature"/>

    <uses-permission android:name="com.appname.permission.C2D_MESSAGE"/>

    <application
        android:name=".BaseApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
        .
        .
        ..
        ..
        </activity>

        <receiver
            android:name=".receivers.MidnightProcessReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="com.alarms.MidnightProcess"/>
            </intent-filter>
        </receiver>

        <receiver
            android:name=".receivers.OnBootReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
    </application>
</manifest>

请帮忙!

提前致谢。

1 个答案:

答案 0 :(得分:1)

由于您需要触发隐式广播,而不是启动服务,因此您需要检索错误类型的PendingIntent。变化:

PendingIntent midnightIntent = PendingIntent.getService(context, 0,
        new Intent("com.alarms.MidnightProcess"), 0);

为:

PendingIntent midnightIntent = PendingIntent.getBroadcast(context, 0,
        new Intent("com.alarms.MidnightProcess"), 0);