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);
}
I'm trying to pass extras provided by SharedPrefereces from an AlarmManager Intent and retrieve it within the Service, instead of access my SharedPreferences while the Service is running which I think demands more memory.
@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);
}
but I'm getting null
on mSecKey
and 0
on idFunc
. I'm not sure if it's the best option, I'd put my SharedPreferences retrieves inside Service's onCreate()
method, but not sure really.
When you call
PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0,
intent, 0);
you may be getting back an existing PendingIntent
that doesn't have your extras in it. You should use
PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
instead. This will ensure that your extras are put in the Intent
. Be aware, however, that the extras will be replaced in all users of this PendingIntent
.