2014-11-06 31 views
-3

我從一個類使用此Android如何從包中獲取/檢索數據?

intent.putExtra("alarmMgr_id", alarm_id); 

我得到的數據中的其他類在包發送數據,但我不知道如何從包安裝IM的截圖檢索:

enter image description here

請指導我如何得到這個數據在其結束紅星; 我想存儲這兩個變量wakelockid = 3,alarmMgr_id = 9作爲整數。

我已經嘗試過,但它沒有爲我工作,所以請看看調試模式的截圖; alarmManager_id每次都返回null。

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

認爲這是對數據進行發送

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類,如果你去,你可以看到即時得到的數據的包,但截圖現在臨危數據A級我不知道如何提取它,你也可以在代碼中看到我嘗試提取值的方法,但都返回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 
} 
} 
+0

您是否嘗試過搜索如何從包中檢索數據,有大量的示例 – 2014-11-06 08:27:04

+0

是的,我確實搜索過,但對我沒有幫助,因爲非解決方案爲我工作。 – mushahid 2014-11-06 08:47:42

回答

1

你可以做這樣的的onCreate您活動

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")); 

,這將做的伎倆。

+0

Rana先生我已經試過這個,但是沒用。 Bundle extras = intent.getExtras(); int alarmManager_id = extras.getInt(「alarmMgr_id」); – mushahid 2014-11-06 08:42:55

+0

如果您在通過意向發送時將數據捆綁在一起,則應該使用捆綁包檢索數據。但是在你的代碼中,你要單獨發送數據,所以你必須通過getIntent() – 2014-11-06 08:59:22

+0

來檢索它,你只是在調用下一個活動意圖時向我展示整個代碼,然後我會知道你實際發送的數據是多少 – 2014-11-06 09:01:22