1
public void onCreate() { 
    super.onCreate(); 
    int idFunc = getApplicationContext().getSharedPreferences(SPREF_NAME,Context.MODE_PRIVATE).getInt("idFunc", 0); 
    String SecKey = getApplicationContext().getSharedPreferences(SPREF_NAME,Context.MODE_PRIVATE).getString("chave", null); 
    Intent intent = new Intent(getApplicationContext(), ServiceEnviaClientes.class); 
    Bundle bundle = new Bundle(); 
    bundle.putString("SecKey", SecKey); 
    bundle.putInt("idFunc", idFunc); 
    intent.putExtras(bundle); 
    PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0, intent, 0); 
    AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE); 
    alarm.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), 10*60000, pintent); 
    initImageLoader(this); 
} 

我試圖從AlarmManager Intent中傳遞SharedPrefereces提供的額外資源,並在Service中檢索它,而不是在服務運行時訪問我的SharedPreferences,而我認爲這需要更多內存。如何在由AlarmManager計劃啓動的服務中檢索Intent Extras?

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    super.onStartCommand(intent, flags, startId); 
    Toast.makeText(this,"task perform in service",Toast.LENGTH_SHORT).show(); 
    Bundle bundle = intent.getExtras(); 
    mIdFunc = bundle.getInt("idFunc"); 
    mSecKey = bundle.getString("SecKey"); 
    ThreadJSONBuild td=new ThreadJSONBuild(); 
    td.start(); 
    Log.d(TAG, "onStartCommand cycle, idFunc: "+mIdFunc+" , SecKey: "+mSecKey); 
    return super.onStartCommand(intent, flags, startId); 
} 

,但我發現在idFuncnullmSecKey0。我不確定這是否是最佳選擇,我會將SharedPreferences檢索到Service的onCreate()方法中,但不確定。

回答

1

當你調用

PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0, 
    intent, 0); 

,你可以回去了現有PendingIntent不會有你的演員在裏面。您應該使用

PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0, 
    intent, PendingIntent.FLAG_UPDATE_CURRENT); 

改爲。這將確保您的附加內容存放在Intent中。但是請注意,這個附加組件將在PendingIntent的所有用戶中被替換。

+0

哦,太好了,我稍後再看看。現在很忙,但看起來像解決了。 (= – DaniloDeQueiroz 2014-11-06 12:29:55

相關問題