没有调用AlarmReceiver

时间:2018-07-04 05:48:02

标签: android

我制作了一个Android应用程序,我想每天在特定时间获取本地通知。我使用AlarmManager和BroadcastReceiver来做到这一点。但是问题是没有调用扩展BroadCastReceiver的AlarmReceiver类。我知道这是stackoverflow中的重复问题。我已经查看了所有答案,并尝试了所有方法,但仍然无法解决问题。

这是我的主要活动

package com.dileepmanuballa224.alarm_test;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                Calendar calendar=Calendar.getInstance();
                calendar.set(Calendar.HOUR,9);
                AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
                Intent intent=new Intent(getApplicationContext(),AlarmReceiver.class);
                PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

    }
}

我的AlarmReceiverClass

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;


public class AlarmReceiver extends BroadcastReceiver {
    private static final String TAG = "alarm_test_check";

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"AlarmReceiver called",Toast.LENGTH_SHORT).show();
        Log.d(TAG, "onReceive: called ");
    }
}

我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dileepmanuballa224.alarm_test">
<uses-permission android:name="android.permission.SET_ALARM"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name=".AlarmReceiver"/>
        </application>

</manifest>

我正在Android“牛轧糖”上测试此应用。

5 个答案:

答案 0 :(得分:1)

要在API 23及更高版本中发出警报,您必须绕过打ze睡。并以与您添加此警报相同的方式,在BroadcastReciver的onReceive()中将警报设置为第二天的第二天的警报。检查下面的示例代码(从此answer中获取)以绕过M及以上的打in睡。

AlarmManager am;

Calendar calendar = Calendar.getInstance();

    calendar.add(Calendar.DAY_OF_MONTH, 1);

    calendar.set(Calendar.HOUR_OF_DAY, 9);
    calendar.set(Calendar.MINUTE, 0);
    calendar.set(Calendar.SECOND, 0);
    calendar.set(Calendar.MILLISECOND, 0);

    // Check if current time is smaller than cal time
    // otherwise notifications will come same day.
    if (System.currentTimeMillis() <= calendar.getTimeInMillis()) {

        SyncAlarm(context, calendar.getTimeInMillis());
    }


private void syncAlarm(Context context, long time) {
Intent intent = new Intent(this, YourBroadcastReceiver.class);

PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

if (Build.VERSION.SDK_INT >= 23) {
    // Wakes up the device in Doze Mode
    am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, // time in millis
    pending);
    } else if (Build.VERSION.SDK_INT >= 19) {
    // Wakes up the device in Idle Mode
    am.setExact(AlarmManager.RTC_WAKEUP, time, pending);
    } else {
    // Old APIs
    am.set(AlarmManager.RTC_WAKEUP, time, pending);
    } 
}

答案 1 :(得分:0)

您必须更改日历的设置时间并将其添加,因此它将在每天的9:00 AM调用AlarmReceiver

Calendar calendar=Calendar.getInstance();
calendar.add(Calendar.HOUR_OF_DAY, (24+9)-(cal.get(Calendar.HOUR_OF_DAY))); // <-- changes this
AlarmManager alarmManager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent=new Intent(getApplicationContext(),AlarmReceiver.class);
PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);

答案 2 :(得分:0)

如记录所示,可以像这样

// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);

// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
        AlarmManager.INTERVAL_DAY, alarmIntent);

这将唤醒设备以在下午2:00左右触发警报,并每天重复一次。

Check official dcoumentation了解详细信息。

希望这会有所帮助。

答案 3 :(得分:0)

安排警报的完整代码。

AlarmScheduler.java放入项目中。
AlarmScheduler.java

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;

import java.util.Calendar;

import static android.content.Context.ALARM_SERVICE;

public class AlarmScheduler {

        public static final int DAILY_REMINDER_REQUEST_CODE = 100;
        public static final String TAG = "AlarmScheduler";

        public static void setReminder(Context context, int hour, int min) {
            Calendar calendar = Calendar.getInstance();

            Calendar setcalendar = Calendar.getInstance();
            setcalendar.set(Calendar.HOUR_OF_DAY, hour);
            setcalendar.set(Calendar.MINUTE, min);
            setcalendar.set(Calendar.SECOND, 0);

            // cancel already scheduled reminders
            cancelReminder(context, AlarmReceiver.class);

            if (setcalendar.before(calendar))
                setcalendar.add(Calendar.DATE, 1);

            // Enable a receiver
            ComponentName receiver = new ComponentName(context, AlarmReceiver.class);
            PackageManager pm = context.getPackageManager();

            pm.setComponentEnabledSetting(receiver,
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);

            Intent intent1 = new Intent(context, AlarmReceiver.class);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, DAILY_REMINDER_REQUEST_CODE, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
            am.setInexactRepeating(AlarmManager.RTC_WAKEUP, setcalendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
        }

        public static void cancelReminder(Context context, Class<?> cls) {

            // Disable a receiver
            ComponentName receiver = new ComponentName(context, cls);
            PackageManager pm = context.getPackageManager();

            pm.setComponentEnabledSetting(receiver,
                    PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                    PackageManager.DONT_KILL_APP);

            Intent intent1 = new Intent(context, cls);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, DAILY_REMINDER_REQUEST_CODE, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
            AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
            am.cancel(pendingIntent);
            pendingIntent.cancel();
        }
    }

您可以通过在“活动/片段”中调用以下方法来安排“警报”。

int mHour = 10, mMin = 20; // Set hours and minute you want to call onReceive method of your BroadcastReceiver.
AlarmScheduler.setReminder(mContext, mHour, mMin); // Here mContext is Context of Activity. If you use this code in Activity then you can pass ActivityName.this and in Fragment you can do getActivity()

AlarmReceiver.java

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;


public class AlarmReceiver extends BroadcastReceiver {
    private static final String TAG = "alarm_test_check";

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context,"AlarmReceiver called",Toast.LENGTH_SHORT).show();
        Log.d(TAG, "onReceive: called ");
    }
}

答案 4 :(得分:0)

我找到了问题的根本原因,并发布了另一个问题。如果您要检查问题,可以从here进行检查

Android Studio自动建议使用错误的权限字符串。它建议使用“ android.permission.SET_ALARM”,但应为“ com.android.alarm.permission.SET_ALARM”。现在一切正常。