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

相关问题