gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:51:02 27 4
gpt4 key购买 nike

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

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)已 Root ,我允许我的应用程序的权限。我的代码有什么问题?如何让它始终返回位于/data/data/com.my.project.app/myprefs.xml 中的实际值?

附言。我找到了解决办法。我变了SharedPreferences pref = con.getSharedPreferences("myprefs", 0);SharedPreferences pref = con.getSharedPreferences("myprefs", MODE_MULTI_PROCESS); 现在可以使用了。

最佳答案

我变了

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

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

现在可以使用了。

关于java - SharedPreferences getSharedPreferences 总是返回以前的值,即使它已被更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47484127/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com