Android : 在服务 class 中更改共享首选项键的值后,它显示默认值

Android : after changing value of Shared Preference key in service class, it displays default value

在服务 class 中更改会话密钥的值后,它显示默认值,同时使用 pendingIntent

调用 activity

0) 这是我的 SharedPreference 文件

public class SessionCounter {
 SharedPreferences pref;
    Editor editor;       
    Context _context;
    int PRIVATE_MODE = 0;        
    private static final String PREF_NAME = "AndroidCounterPref";   
    private static final String sessionCounter = "session_Counter";

    public SessionCounter(Context context){
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    public void createCounterSession(Integer name){        
        editor.putInt(sessionCounter, name);
        editor.commit();
    }

    public int getCounterSession(){         
        int val=pref.getInt(sessionCounter, 0);
        return val; 
    }

}

1) 这是我的服务class CaptureImagesService class

    public static int COUNTER=-1;  

public int onStartCommand(Intent intent, int flags, int startId){                   
    context=this;
    this.startId=startId;       
    randomTime=getRandom(time_interval) * 30 * 1000 ;       
    timer1=new Timer();
    timer1.schedule(new TimerTask() {

        @Override
        public void run() {
            sessionCounter.createCounterSession(1);
            startAlarm();               
        }
    }, randomTime);     
    return START_STICKY;
}

 public void startAlarm()
 {  
      sessionCounter.createCounterSession(1);
         manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);         

            Intent alarmIntent = new Intent(CaptureImagesService.this, HomeActivity.class);
            alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            pendingIntent=PendingIntent.getActivity(getBaseContext(), 1001, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Log.d("CaptureImagesService", "after pendingIntent");    

            long whenToTrigger=Calendar.getInstance().getTimeInMillis();               
            manager.set(AlarmManager.RTC_WAKEUP, whenToTrigger, pendingIntent);
 } 

2) 这是我的 MainActivity onResume 方法,我想在其中使用此值,如果 COUNTER 的值为 1,则执行操作

protected void onResume() {
    super.onResume();       
    if(sessionCounter.getCounterSession()==1)
          WakeUpDevice();
          askForPicture();  
          //etc
    }
}

public void addCall(View v) {       

    if (sfStore.getString(KEY_CURRENT_CALL_ID, "-1").equalsIgnoreCase("-1")) {
          startService(new Intent(getBaseContext(), CaptureImagesService.class));
    }

请帮我解决这个问题

我使用了 getBroadcast 而不是 getActivity

public class CaptureBroadcast extends BroadcastReceiver{

public static boolean counterNew;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub  
    counterNew=true;        //and check if counter is true then
                            // do some code in onResume method of HomeActivity
    contextStart=context;

    Intent intentHome=new Intent(context, HomeActivity.class);
    intentHome.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intentHome);
}
}

在 HomeActivity onResume

protected void onResume() {
    super.onResume();   

    if(CaptureBroadcast.counterNew==true){
                 // some code
     }
}