Android如何从捆绑中获取/检索数据?

时间:2014-11-06 08:24:37

标签: android bundle android-bundle

我使用此

从一个班级发送数据
 intent.putExtra("alarmMgr_id", alarm_id);

我在捆绑中的另一个类中获取数据但我不知道如何从捆绑中检索它并附上屏幕截图:

enter image description here

请指导我如何获得最终有红星的数据; 我想将这两个变量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
}
} 

1 个答案:

答案 0 :(得分:1)

您可以在onCreate of Activity

中执行此操作
  int alarmMgr_id;

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

但在看到你的代码之后,我意识到这一行代码

service.putExtra("alarmMgr_id", alarm_id);

你发送的是一个字符串变量alarm_id,并在你正在尝试检索的服务中

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

即你正在使用getInt(),所以只需将此行改为

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

这样就可以了。