1

I doing project on android services ,In my project I'm using a button to start chronometer,I need to stay the chronometer if i closed the app can you guys help me?

my code for Main Activity.java is

public void onClick(View v) {

   if(v==let_break){
        startService(new Intent(MainActivity.this,runBackground.class));

        Toast.makeText(getApplicationContext(),"I've got this!",Toast.LENGTH_SHORT).show();

        chronometer.setVisibility(View.VISIBLE);
        chronometer.start();
        timer_running=true;
        //let_break.setText("stop");
        let_break.setVisibility(View.INVISIBLE);
        stop_break.setVisibility(View.VISIBLE);
    }
    if(v==stop_break){
        chronometer.stop();
        timer_running=false;
        let_break.setVisibility(View.VISIBLE);
        stop_break.setVisibility(View.INVISIBLE);
        stopService(new Intent(MainActivity.this,runBackground.class));
  }}

my service code is

public class runBackground extends Service {


@Override
public void onCreate() {
    super.onCreate();
}

@Override
public int onStartCommand(Intent intent,int flags, int startId) {
    Toast.makeText(runBackground.this,"service started",Toast.LENGTH_LONG).show();

    return START_STICKY;
}

@Override
public void onDestroy() {
    Toast.makeText(runBackground.this,"service stoped.",Toast.LENGTH_LONG).show();

}
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}}

My Manifest is

<application>
 <service android:name=".runBackground" android:exported="false"></service>
</application>

when i click a start button Toast showing service is started stop button showing it stopped but when look at the running process background it shows app service is running but chronometer and the button are reset. how should stay the action and chronometer even if i close the app.

Sukesh
  • 184
  • 2
  • 17

2 Answers2

1

Since you're stating that the values are reset between executions - it seems like you need to persist your data onto local storage. Easiest would be to use SharedPreferences. You can add a simple helper class:

public class MySharedPreferences {

private static final String PREF_FILENAME = "mybestprefs";

public static void putPrefLong(Context context, String key, long value){
    SharedPreferences prefs = context.getSharedPreferences(PREF_FILENAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putLong(key, value);
    boolean commitResult = editor.commit();
    if (!commitResult){
        Log.e(TAG, "commit failed. key - "+key+", value - "+value);
    }
}

public static long getPrefLong(Context context, String key, long defValue){
    SharedPreferences prefs = context.getSharedPreferences(PREF_FILENAME, Context.MODE_PRIVATE);
    return prefs.getLong(key, defValue);
}
}

inside your chronometer.start():

startTimestamp = System.currentTimeMillis();
MySharedPreferences.putPrefLong(context, "start_ts", startTimestamp);

inside your chronometer.stop():

// calculate elapsed time:
long elapsedTimeSinceStart = System.currentTimeMillis() - MySharedPreferences.getPrefLong(context, "start_ts", 0);
Arseny Levin
  • 664
  • 4
  • 10
  • Any way Thanks bro it not working I got some ideas on your code – Sukesh Jul 01 '18 at 09:42
  • Of course it won't work as is, because I have no idea how your `chronometer` code looks like. Bottom line - you simply need to persist your data. Gave you a starting point... – Arseny Levin Jul 01 '18 at 09:46
0

Run the service in a separate process

   android:process=":something"
Amirhosein
  • 4,266
  • 4
  • 22
  • 35