I have a service that is started when the main activity starts. I call the Service using this code
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();
}
The Service has the following code:
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();
}
}
}
Every 5 seconds in my service runs method check_pref(), which checks login in another app (com.my.project.app) and prints it in Logcat. But even login has been changed it returns same value as previously. Only if I restart app it returns actual value. My device(android 5.1.1) is rooted and I allowed permissions for my app. Whats wrong with my code? How to make it always return the actual value, which located in /data/data/com.my.project.app/myprefs.xml ?
PS. I found a solution. I changed
SharedPreferences pref = con.getSharedPreferences("myprefs", 0);
to
SharedPreferences pref = con.getSharedPreferences("myprefs", MODE_MULTI_PROCESS);
and it works now.