-3

我正在使用这个从一个班级发送数据

 intent.putExtra("alarmMgr_id", alarm_id);

我正在获取捆绑包中其他类的数据,但我不知道如何从捆绑包中检索它,我附上了屏幕截图:

在此处输入图像描述

请指导我如何获取这些以红星结尾的数据;我想将这两个变量 wakelockid=3, alarmMgr_id=9 存储为整数。

我已经尝试过了,但它对我不起作用,所以请查看调试模式截图;alarmManager_id 每次都返回 null。

 Bundle extras = intent.getExtras(); 
 int alarmManager_id = extras.getInt("alarmMgr_id");

将其视为发送数据的 A 类

public class AlarmBroadcastReciever extends WakefulBroadcastReceiver {
// The app's AlarmManager, which provides access to the system alarm
// services.
private AlarmManager alarmMgr;
// The pending intent that is triggered when the alarm fires.
private PendingIntent alarmIntent;

@Override
public void onReceive(Context context, Intent intent) {
    String alarm_id = intent.getAction();
    Intent service = new Intent(context, AlarmSchedulingService.class);
    service.putExtra("alarmMgr_id", alarm_id);
    // Start the service, keeping the device awake while it is launching.
    startWakefulService(context, service);
}


@SuppressLint("NewApi")
public void setAlarm_RepeatOnce(Context context, int year, int month,
        int date, int hours, int minutes, TaskModel currentTask) {
    try {
        alarmMgr = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);

        int alarm_id = currentTask._id;
        Intent intent = new Intent(context, AlarmBroadcastReciever.class);
        intent.setAction(String.valueOf(alarm_id));  

        alarmIntent = PendingIntent.getBroadcast(context, alarm_id , intent,  
                  PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.DATE, date);
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.HOUR_OF_DAY, hours);
        calendar.set(Calendar.MINUTE, minutes);

        alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                alarmIntent);

        // Enable {@code AlarmBootBroadCastReceiver} to automatically
        // restart the alarm
        // when the
        // device is rebooted.
        ComponentName receiver = new ComponentName(context,
                AlarmBootBroadcastReceiverRepeatOnce.class);
        PackageManager pm = context.getPackageManager();

        pm.setComponentEnabledSetting(receiver,
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP);
    } catch (Exception ex) {
        String e = ex.getLocalizedMessage();
    }
}
}

这作为 B 类,现在接收数据如果您转到屏幕截图,您可以看到我正在获取捆绑包中的数据,但我不知道如何提取它,您还可以在代码中看到我尝试过的方法提取值,但全部返回 null。

public class AlarmSchedulingService extends IntentService {
DatabaseHelper db;
Context mContext;
public AlarmSchedulingService() {
    super("SchedulingService");
}

// An ID used to post the notification.
public int NOTIFICATION_ID;//= 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
//Bundle extras;


@SuppressLint("NewApi") @Override
protected void onHandleIntent(Intent intent) {
    try{


       // Get passed values
    Bundle extras = intent.getExtras();
    int uniqueID = extras.getInt("alarm_id");
    int alarmMgr_id =  extras.getInt("alarmMgr_id");  
    int alarmMgr_idd = extras.getParcelable("alarmMgr_id");



   // NOTIFICATION_ID = alarmMgr_id;//uniqueID;
   // sendNotification(alarmMgr_id);
    // Release the wake lock provided by the BroadcastReceiver.
    AlarmBroadcastReciever.completeWakefulIntent(intent);
    }
    catch(Exception e)
    {
        String ex = e.getLocalizedMessage();
    }
}
// Post a notification
private void sendNotification(  int alarm_id) {
    //code for notificaiton generation
}
} 
4

1 回答 1

1

您可以在 Activity 的 onCreate 中执行此操作

  int alarmMgr_id;

  if(getIntent().getIntExtra("alarmMgr_id",yourDefaultValue)!=null)
  alarmMgr_id=getIntent().getIntExtra("alarmMgr_id",yourDefaultValue);

但是看到你的代码后,我意识到在这行代码中

service.putExtra("alarmMgr_id", alarm_id);

您正在发送作为 String 变量的 alarm_id 并在您尝试像这样检索它的服务中

 int alarmMgr_id =  extras.getInt("alarmMgr_id");  

即您正在使用 getInt(),所以只需将您的这一行更改为

 int alarmMgr_id =  Integer.parseInt(extras.getString("alarmMgr_id"));  

这可以解决问题。

于 2014-11-06T08:27:01.713 回答