广播接收器不会在Android棒棒糖设备上触发

时间:2014-12-10 07:44:50

标签: android broadcastreceiver alarmmanager android-5.0-lollipop android-pendingintent

以下是设置广播接收器的代码。

        PendingIntent pendingIntentScheduler = PendingIntent.getBroadcast(context, REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);            
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntentScheduler);

和BROADCASTRECIEVER类的代码

@Override
public void onReceive(Context context, Intent intent) {
     this.context = context;        
     Log.d("onReceive", "this is broadcast reciever");        

}

Android Manifest Permisssions。

  <uses-permission android:name="android.permission.WAKE_LOCK" />
  <application>
  <receiver android:name=".name.recieverName" >
        <intent-filter>
            <action android:name="com.intentIWantToOpenOnRecievingBroadcast" />
        </intent-filter>
    </receiver>
   </application>

此代码与api的工作正常至19岁。它不适用于Lollipop api 21.请问有人发现任何问题吗?。

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

试试这个

AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

        Intent intent = new Intent(context, AlarmReceiver.class);
        PendingIntent pi = PendingIntent.getBroadcast(context, AlarmActive, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        if (Build.VERSION.SDK_INT >= 19)
            am.setExact(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), pi);
        else
            am.set(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), pi);