2017-08-03 46 views
0

鬧鐘管理器在我的手機「Kitkat」上不起作用,但它在Emulator GenyMotion API上工作25我想知道什麼是錯的?通知管理器在Kitkat中不起作用

我稱這種現象主要活動

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

最後這裏是清單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> 

和gradle這個 應用插件: 'com.android.application'

機器人{ compileSdkVersion 25 buildToolsVersion 「25.0.2」 defaultConfig { 的applicationID 「com.hema.engshow」 的minSdkVersion 16 targetSdkVersion 25 的versionCode 1 的versionName 「1.0」 } buildTypes { 釋放{ minifyEnabled假 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'} 

我希望有人幫助,因爲我到處找我想不通的問題但徒勞無益。

回答

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

有用於AlarmManager一些方法改變爲不同的API級別嘗試上面的代碼,它的一個時間報警,你可以找到在Android文檔重複報警相同