Alarm Manager在某些设备上运行正常,而其他设备无效

时间:2018-03-12 15:55:08

标签: java android alarmmanager android-alarms

Alarm Manager在某些设备(版本)中运行正常,而其他(版本)不起作用。它有时会发射,有时却不发射。 我做了很多搜索,但它仍然在某些设备(版本)和其他(版本)工作正常。 这是我使用的代码

public static void startHeadService(int h, int m, int id , int azkar_type) {
        Intent intent = new Intent(ctx, AlarmBroadcastReciever.class);
        intent.setAction("azkar_b");
        intent.putExtra("azkar_type",azkar_type);
        intent.putExtra("h",h);
        intent.putExtra("m",m);
        intent.putExtra("id",id);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, id, intent, 0);
        if (pendingIntentList != null && !pendingIntentList.containsKey(id))
            pendingIntentList.put(id, pendingIntent);
        long _alarm = 0;
        Calendar now = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        //calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, h);
        calendar.set(Calendar.MINUTE, m);
        long asd = AlarmManager.INTERVAL_DAY;
        if (calendar.getTimeInMillis() <= now.getTimeInMillis())
            _alarm = calendar.getTimeInMillis() + (AlarmManager.INTERVAL_DAY + 1);
        else
            _alarm = calendar.getTimeInMillis();

        alarmManager = (AlarmManager) ctx.getSystemService(Context.ALARM_SERVICE);
        //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
        //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, _alarm, AlarmManager.INTERVAL_DAY, pendingIntent);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, _alarm, pendingIntent);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, _alarm, pendingIntent);
        }else {
            alarmManager.set(AlarmManager.RTC_WAKEUP, _alarm, pendingIntent);
        }

        //alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,_alarm,2*60*1000,pendingIntent);
        /*Context context = getActivity();
        context.startService(new Intent(context, HeadService.class));*/
    }

这是我的BroadcastReceiver

public class AlarmBroadcastReciever extends BroadcastReceiver {

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

        String azkarAction="azkar_b";

        if(azkarAction.equals(intent.getAction())) {
            int azkar_type = intent.getIntExtra("azkar_type",0);
            int h = intent.getIntExtra("h", 0);
            int m = intent.getIntExtra("m", 0);
            int id = intent.getIntExtra("id", 0);

            Intent intent1 = new Intent(context, AzkarService.class);
            intent1.setAction("azkar");
            intent1.putExtra("azkar_type", azkar_type);
            intent1.putExtra("h", h);
            intent1.putExtra("m", m);
            intent1.putExtra("id", id);

            SettingsAzkarFragment.ctx=context;
            SettingsAzkarFragment.startHeadService(h,m,id,azkar_type);
            context.startService(intent1);
        }
    }
}

这是我的AndroidManifest.xml

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <application....>
  ...
  ....
  <receiver android:name=".BroadcastRecievers.AlarmBroadcastReciever">
            <intent-filter>
                <action android:name="azkar_b" />
            </intent-filter>
        </receiver>


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

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
  ...
  ....
  </application>

并且我还处理了BOOT_COMPLETED案例并再次重置所有警报

0 个答案:

没有答案