简体   繁体   English

警报管理器未触发广播接收器

[英]Alarm Manager didn't Triggger to Broadcast Receiver

I'm going to build an Application with Alarm feature to remind the patients to take medicine on time. 我将构建一个带闹钟功能的应用程序,以提醒患者按时服药。

But current issue is the alarm manager didn't trigger on the XiaoMi phone, and it work on the samsung note 4 in Android 6.0. 但目前的问题是警报管理器没有在小米手机上触发,而且它在Android 6.0中的三星音符4上工作。 And before implement the code to my project, I had create a new project and using the same code to test it. 在将代码实现到我的项目之前,我创建了一个新项目并使用相同的代码来测试它。 In the new project the code work perfectly as long as the Autostart permission has grant. 在新项目中,只要自动启动权限授予,代码就能完美运行。 The alarm will trigger on time, and even restart the phone, the boot receiver will also work recreate the alarm, but it only didn't work on my application. 警报将按时触发,甚至重新启动手机,启动接收器也会重新启动警报,但它只对我的应用程序无效。

And now below is my code. 现在下面是我的代码。

OnAlarmReceiver.java OnAlarmReceiver.java

public class OnAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("MyApp", "Received wake up from alarm manager.");

        long rowid = intent.getExtras().getLong(RemindersDbAdapter.KEY_ROWID);

        WakeReminderIntentService.acquireStaticLock(context);

        Intent i = new Intent(context, ReminderService.class);
        i.putExtra(RemindersDbAdapter.KEY_ROWID, rowid);
        context.startService(i);

    }
}

ReminderManager.java ReminderManager.java

public class ReminderManager {

    private Context mContext;
    private AlarmManager mAlarmManager;

    public ReminderManager(Context context) {
        mContext = context;
        mAlarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    }

    public void setReminder(Long taskId, Calendar when) {

        Intent i = new Intent(mContext, OnAlarmReceiver.class);
        i.putExtra(RemindersDbAdapter.KEY_ROWID, (long)taskId);

        int broadcastID = Integer.parseInt(taskId+""+when.getTime().getDate()+""+when.getTime().getYear()+""+taskId);
        broadcastID = (int)when.getTimeInMillis();
        System.out.println("Alarm when : "+ when.getTimeInMillis());

        PendingIntent pi = PendingIntent.getBroadcast(mContext, broadcastID, i, PendingIntent.FLAG_ONE_SHOT);
        System.out.println("Alarm Set ID : "+broadcastID);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);
            AlarmManager.AlarmClockInfo ac= new AlarmManager.AlarmClockInfo(when.getTimeInMillis(), pi);
            mAlarmManager.setAlarmClock(ac, pi);
            System.out.println("Android 6.0 Marshmallow and above.");

        }else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
            System.out.println("Android 4.4 Kikkat and above.");
        }else{
            mAlarmManager.set(AlarmManager.RTC_WAKEUP, when.getTimeInMillis(), pi);
            System.out.println("Android 4.3 Jelly Bean and below.");
        }
    }
}

Manifest file: 清单文件:

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

Forgot to mention, i had already register the .OnAlarmReceiver in manifest file 忘了提,我已经在清单文件中注册了.OnAlarmReceiver

<receiver android:name=".OnBootReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver android:name=".OnAlarmReceiver" />

My main issue is with same code it work on the new project i had create to do testing, but didn't work on the my main project. 我的主要问题是它使用相同的代码来处理我创建的用于测试的新项目,但是在我的主项目中没有用。

Can someone help me out. 有人可以帮我吗。 Thanks in advance. 提前致谢。

在清单中注册OnAlarmReceiver

 <receiver android:name=".OnAlarmReceiver" />
 <receiver android:name=".OnAlarmReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"/>
        </intent-filter>
 </receiver>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM