SharedPreferences getSharedPreferences总是返回先前的值,即使它已被更改

时间:2017-11-25 08:47:26

标签: java android service timer sharedpreferences

我有一个主要活动开始时启动的服务。我使用此代码调用服务

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService(new Intent(this, CheckService.class));
    finish();
}

该服务具有以下代码:

public class CheckService extends Service {

private static final String TAG = "CheckService";
WakeLock wakeLock;



public CheckService() {
    // TODO Auto-generated constructor stub
}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    return START_STICKY;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    // TODO Auto-generated method stub
    Intent restartService = new Intent(getApplicationContext(),
            this.getClass());
    restartService.setPackage(getPackageName());
    PendingIntent restartServicePI = PendingIntent.getService(
            getApplicationContext(), 1, restartService,
            PendingIntent.FLAG_ONE_SHOT);

    //Restart the service once it has been killed android


    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 100, restartServicePI);

}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();

    PowerManager pm = (PowerManager) getSystemService(this.POWER_SERVICE);

    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "DoNotSleep");

    Log.e("Checker", "Service Created");

    Timer myTimer = new Timer();
    myTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            check_pref();

        }
    }, 0, 5000);


}

@Override
@Deprecated
public void onStart(Intent intent, int startId) {
    // TODO Auto-generated method stub
    super.onStart(intent, startId);



}


@Override
public void onDestroy() {
    super.onDestroy();
    sendBroadcast(new Intent("YouWillNeverKillMe"));
}



public void check_pref( ) {
    Context con;
    Log.e("check_pref", "Restarting");

    try {

        Process   p = Runtime.getRuntime().exec("su");
        DataOutputStream dos = new DataOutputStream(p.getOutputStream());
        dos.writeBytes("chmod -R 0777 /data/data/com.my.project.app/\n");
        dos.writeBytes("exit\n");
        dos.flush();
        dos.close();
        p.waitFor();
        p.destroy();
        con = createPackageContext("com.my.project.app", 0);
        SharedPreferences pref = con.getSharedPreferences("myprefs", 0);
        String login_id = pref.getString("login", "");
        System.out.println(login_id);

    }   catch (PackageManager.NameNotFoundException e)

    {
        Log.e("check_pref", e.toString());
    }


    catch (NullPointerException e){
        e.printStackTrace();
    }
    catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
    catch (InterruptedException e){
    e.printStackTrace();
}

}
}

我的服务中每5秒运行一次方法check_pref(),它检查另一个应用程序(com.my.project.app)中的登录并将其打印在Logcat中。但即使登录已被更改,它返回与以前相同的值。只有我重新启动应用程序才会返回实际值。我的设备(Android 5.1.1)已植根,我允许我的应用程序的权限。我的代码有什么问题?如何让它始终返回实际值,该值位于/data/data/com.my.project.app/myprefs.xml?

PS。我找到了解决方案。我变了 SharedPreferences pref = con.getSharedPreferences("myprefs", 0);SharedPreferences pref = con.getSharedPreferences("myprefs", MODE_MULTI_PROCESS);它现在有效。

1 个答案:

答案 0 :(得分:1)

我改变了

SharedPreferences pref = con.getSharedPreferences("myprefs", 0); 

SharedPreferences pref = con.getSharedPreferences("myprefs", MODE_MULTI_PROCESS); 

现在可以使用了。