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提供的额外内容并在服务中检索它,而不是在服务运行时访问我的SharedPreferences,我认为这需要更多内存。
@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);
}
但我在null
上mSecKey
和0
上idFunc
获得了onCreate()
。我不确定它是否是最佳选择,我将我的SharedPreferences检索到服务的{{1}}方法中,但不确定。
答案 0 :(得分:1)
致电时
PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0,
intent, 0);
您可能会收回现有的PendingIntent
,但其中没有您的额外内容。你应该使用
PendingIntent pintent = PendingIntent.getService(getApplicationContext(), 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
代替。这将确保您的额外内容放在Intent
中。但请注意,附加内容将在此PendingIntent
的所有用户中替换。