通知管理器在Kitkat中不起作用
闹钟管理器在我的手机“Kitkat”上无法使用,但它在Emulator GenyMotion API 25上工作我不知道什么是错的?!
我在主要活动中称此为
PollReceiver.scheduleAlarms(getApplicationContext());
这里是涉及的类:
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);
}
}
这是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());
}
}
最后这里是清单文件:
<?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>
和gradle apply插件:'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'}}}
依赖关系{
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'}
我希望有人帮我弄清楚这个问题,因为我到处搜索,但徒劳无功。
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/85299.html上一篇: notification manager doesn't work in Kitkat
下一篇: Igot this error package com.google.firebase.messaging does not exist