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.