在广播接收器中获取空操作



当我尝试使用警报调用广播接收器时,得到空操作。

    String action = k2.getAction();

获取操作 null。

我如何称呼警报接收器:

@SuppressLint("NewApi")
    private void setAlarm(String remCategory,Calendar calNow, Context context, int request_code) {
        init(context);
        Log.i("Inside setAlarm,","Yes");
        Log.i("setAlarm Category",remCategory);
        Intent intent = new Intent(context, AlarmReceiver.class);
        intent.putExtra("Rem_cat",remCategory);
        intent.putExtra("Rem_calling_class",UpdateTables.this.getClass().getSimpleName());
        long alarmtime=calNow.getTimeInMillis();
        System.out.println("Reminder Time is "+calNow.get(Calendar.HOUR_OF_DAY)+" "+calNow.get(Calendar.MINUTE));
        Log.i("Target",Long.valueOf(alarmtime).toString());
        //final int _id = (int) System.currentTimeMillis();
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, request_code, intent, PendingIntent.FLAG_ONE_SHOT);
        int currentapiVersion = android.os.Build.VERSION.SDK_INT;
        if (currentapiVersion < android.os.Build.VERSION_CODES.KITKAT){
            alarmManager.set(AlarmManager.RTC_WAKEUP, alarmtime, pendingIntent);
        } else {
            if (currentapiVersion < android.os.Build.VERSION_CODES.M) {
                alarmManager.setExact(AlarmManager.RTC_WAKEUP, alarmtime, pendingIntent);
            } else {
                alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmtime, pendingIntent);
            }
        }
    }

报警接收器

public class AlarmReceiver extends BroadcastReceiver {
    public  String TAG=AlarmReceiver.this.getClass().getSimpleName();
    @Override
    public void onReceive(Context k1, Intent k2) {
        // TODO Auto-generated method stub
        Log.i("Alarm Received","Yes");
        String action = k2.getAction();
        String reminder_cat=k2.getExtras().getString("Rem_CAT");
        String calling_class=k2.getExtras().getString("Rem_calling_class");
        Calendar calendar=Calendar.getInstance();
        System.out.println(TAG+ calendar.getTime());
        System.out.println(TAG+" calling class "+calling_class);
        Intent i=new Intent(k1,AlertDialogActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra("Rem_CAT",reminder_cat);
        Log.d(TAG,reminder_cat);
        k1.startActivity(i);
    }
}

清单:

 <receiver android:name=".AlarmReceiver"
            android:enabled="true"
            android:process=":remote">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

在您的AlarmReceiver中,检索的键值并不相同。

更改此行:

String reminder_cat=k2.getExtras().getString("Rem_CAT");

对此:

String reminder_cat=k2.getExtras().getString("Rem_cat");

这应该可以让您获得正确的意图附加功能,并允许接收器不会失败。

最新更新