AlarmReceiver is not being called

问题: I've made an android app and I want to get local notifications every day at a specific time. I used AlarmManager and BroadcastReceiver to do this. But the problem is the Al...

问题:

I've made an android app and I want to get local notifications every day at a specific time. I used AlarmManager and BroadcastReceiver to do this. But the problem is the AlarmReceiver class which extends BroadCastReceiver is not being called. I know this is a repeated question in stackoverflow. I've gone through all the answers and tried everything, but still not able to solve the issue.

Here is my Main Activity

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);

    }
}

My 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 ");
    }
}

My Manifest file

<?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>

I'm testing this app on Android 'Nougat'.


回答1:

To have alarm in API 23 and greater you have to bypass doze. And to repeat that alarm set alarm for next day at 9 in BroadcastReciver's onReceive() same way you added this alarm. Check sample code below (taken from this answer) to bypass doze in M and above.

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);
    } 
}

回答2:

You have to change set timing of calendar and add this, so it will called AlarmReceiver at 9:00 AM every day

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);

回答3:

As documented same can be done like this

// 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);

This will wake up the device to fire the alarm at approximately 2:00 p.m., and repeat once a day at the same time.

Check official dcoumentation for detailed information.

Hope this helps.


回答4:

The complete code to schedule Alarm.

Put AlarmScheduler.java in your project.
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();
        }
    }

You can schedule Alarm by calling following method in your Activity/Fragment.

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 ");
    }
}

回答5:

I found the root cause of the problem and posted another ques. If you want to check the question,you can check from here .

Android studio auto-suggested the wrong permission String. It suggested "android.permission.SET_ALARM", but it should be "com.android.alarm.permission.SET_ALARM". Now everything is working fine.

  • 发表于 2018-07-05 20:20
  • 阅读 ( 312 )
  • 分类:sof

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除