notification manager doesn't work in Kitkat

Alarm manager doesn't work on my cell phone "Kitkat" but it does work on Emulator GenyMotion API 25 I wonder what's the wrong ?!

I call this in main activity

PollReceiver.scheduleAlarms(getApplicationContext());

And here are the classes involved:

package com.hema.engshow;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;

import java.util.Calendar;

import static android.content.Context.ALARM_SERVICE;

public class PollReceiver extends BroadcastReceiver {
    static AlarmManager mAlarmManager;

    @Override
    public void onReceive(Context ctxt, Intent i) {
        scheduleAlarms(ctxt);
    }

    static void scheduleAlarms(Context ctxt) {
        Calendar mCalendar = Calendar.getInstance();
        mCalendar.set(Calendar.HOUR_OF_DAY,14);
        mCalendar.set(Calendar.MINUTE,47);
        mCalendar.set(Calendar.SECOND,10);

        Intent intent=new Intent(ctxt,AlarmReceiver.class);
        PendingIntent broadcast=   PendingIntent.getBroadcast(ctxt,
                101,intent,PendingIntent.FLAG_UPDATE_CURRENT);

        mAlarmManager=(AlarmManager) ctxt.getSystemService(ALARM_SERVICE);

        mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,mCalendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,broadcast);
    }
}

And this is the alarmReciever:

package com.hema.engshow;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.TaskStackBuilder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.text.format.Time;

import java.util.logging.FileHandler;

import static android.content.Context.NOTIFICATION_SERVICE;

/**
 * Created by hema on 7/30/2017.
 */

public class AlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        Intent notificationIntent = new Intent(context, MainActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context,100,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle("Demo App Notification")
                .setContentText("New Notification From Demo App..")
                .setTicker("New Message Alert!")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true);


        notificationManager.notify(0, builder.build());
    }
}

And finally here is the manifest file:

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

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

    <uses-permission android:name="com.android.alarm.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">

        </activity>
        <activity android:name=".Vocab">
        </activity>
        <activity android:name=".splash">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

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


        <receiver android:name=".AlarmReceiver" />
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
        <receiver android:name="PollReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>
    </application>

</manifest>

and the gradle apply plugin: 'com.android.application'

android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.hema.engshow" minSdkVersion 16 targetSdkVersion 25 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }

dependencies {

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:recyclerview-v7:25.0.0'
compile 'com.android.support:cardview-v7:25.0.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:design:23.1.1'
compile 'com.google.code.gson:gson:2.6.2'}

I hope someone helps me figure out the problem because I searched everywhere but in vain.


if (Build.VERSION.SDK_INT >= 23) {
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calobj.getTimeInMillis(), pendingIntent);
        } else if(Build.VERSION.SDK_INT >= 19){
            alarmManager.setExact(AlarmManager.RTC_WAKEUP, calobj.getTimeInMillis(), pendingIntent);
        } else{
            alarmManager.set(AlarmManager.RTC_WAKEUP, calobj.getTimeInMillis(), pendingIntent);
        }

对于不同API级别的AlarmManager有一些方法更改,请尝试使用上述代码,对于一次性警报,您可以在Android文档中找到相同的重复警报

链接地址: http://www.djcxy.com/p/85300.html

上一篇: Google移动视觉库11.8.0无法识别

下一篇: 通知管理器在Kitkat中不起作用