When I start the app the value in the counter is posted for every 1000ms. The service is kept running even when the app is closed or screen is locked but when i start the app again the service is recreated and I see the counter starting from 0 and it shuffles between the old counter value and the new.
So I see 2 values - New counter & Old counter values updating at the same time and overlap. How do I eliminate the new counter (or stop the service from recreating)
This is my service running in the background
package com.myexample.serviceexample;
import java.util.Calendar;
import java.util.Date;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
public class myservice extends Service {
private static final String TAG = "serviceexample";
public static final String BROADCAST_ACTION = "com.myexample.serviceexample";
private final Handler handler = new Handler();
Intent intent;
int counter = 0;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
intent = new Intent(BROADCAST_ACTION);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handler.removeCallbacks(sendUpdatesToUI);
handler.postDelayed(sendUpdatesToUI,1000);
return Service.START_STICKY;
}
private Runnable sendUpdatesToUI = new Runnable() {
public void run() {
DisplayLoggingInfo();
handler.postDelayed(this,1000);
}
};
private void DisplayLoggingInfo() {
Log.d(TAG, "entered DisplayLoggingInfo");
intent.putExtra("time","some text" );
intent.putExtra("counter", String.valueOf(++counter));
sendBroadcast(intent);
}
}
This is my activity code
package com.myexample.serviceexample;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private Intent intent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
intent = new Intent(this, myservice.class);
}
public void onClick(View view) {
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
updateUI(intent);
}
};
public void onResume() {
super.onResume();
startService(intent);
registerReceiver(broadcastReceiver, new IntentFilter(myservice.BROADCAST_ACTION));
}
public void onStop() {
super.onStop();
startService(intent);
registerReceiver(broadcastReceiver, new IntentFilter(myservice.BROADCAST_ACTION));
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(broadcastReceiver);
stopService(intent);
}
private void updateUI(Intent intent) {
String counter = intent.getStringExtra("counter");
String time = intent.getStringExtra("time");
Log.d(TAG, counter);
Log.d(TAG, time);
TextView txtDateTime = (TextView) findViewById(R.id.textView1);
TextView txtCounter = (TextView) findViewById(R.id.textView2);
txtDateTime.setText(time);
txtCounter.setText(counter);
}
}