Iam坚持了超过4个小时的问题,无法从Service类中的sharedPrefernce传递更新的值。
public class Background extends Service {
.....
.....
public int onStartCommand(Intent intent, int flags, int startId)
{
return Service.START_STICKY;
}
....
....
private void callService() {
String prefCount=getSharedData("list_update_count");
myb = new myBroad();
Context context = this.getApplicationContext();
myb.SetAlarm(context,prefCount);
}
....
....
public class myListener extends PhoneStateListener
{
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
if (state == TelephonyManager.CALL_STATE_IDLE)
{
String phoneNumber = incomingNumber;
callService();
}
}
}
我的BroadcastReceiver ------------------------------------------
public class myBroad extends BroadcastReceiver{
.....
.....
.....
public void onReceive(Context context, Intent intent) {
....
.....
}
public void SetAlarm(Context context,String prefCount){
//here the prefCount is not receiving the updated value!!
buildToast(prefCount,context);
}
.....
}
当我在强制停止后启动应用程序时,可以正确获取来自sharedPreference的更新值。谢谢 ...
你有尝试过吗?
SharedPreferences preferences = getSharedPreferences("preferences", Context.MODE_MULTI_PROCESS);
抱歉,Global是我自己的类,其中包含我的项目中使用的常量,将Global.PREFERENCES替换为您自己的SharedPreferences名称。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句