-2

I want to make an application that can update automatically by time when some method excuted. I used Handler to make it update automatically by time. when the time has come, it will update data to firebase. it can work when the app is not closed. but when i closed the app before the specified update time, the handler does not work. Although I've used Services that work to keep the application running.

so far this is my code :

AutoUpdate.java

public class AutoUpdate extends AppCompatActivity {
    TextView textDateTime;
    TextView textcurrentDateTime;
    DatabaseReference mDatabase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_update);

        String currentDate = DateFormat.getDateTimeInstance().format(new Date());
        textcurrentDateTime.setText(currentDate);

        final Handler handler=new Handler();
        final Runnable updateTask=new Runnable() {
            @Override
            public void run() {
                updateCurrentTimeToFirebase();
                handler.postDelayed(this,7000);
                handler.removeCallbacks(this);
            }
        };

        handler.postDelayed(updateTask,7000);
        startService(new Intent(this, MyService.class));
    }

    public  void updateCurrentTimeToFirebase() {
        String currentDate = DateFormat.getDateTimeInstance().format(new Date());
        mDatabase = FirebaseDatabase.getInstance().getReference();
        String id = mDatabase.push().getKey();
        mDatabase.child("autoUpdate").child(id).child("updateTime").setValue(currentDate);
    }
}

Myservice.java

public class MyService extends Service {
    public MyService() {
    }

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

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }
}

please i need your help.. Thankyou so much..

Sushin Pv
  • 1,826
  • 3
  • 22
  • 36
Shafa99
  • 3
  • 4

1 Answers1

0

You are creating a background service but all the operation is still doing in the foreground activity only

Change your code as follows

AutoUpdate.class

public class AutoUpdate extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_update);

        if (!isMyServiceRunning(MyService.class)) {
            Intent intent = new Intent(AutoUpdate.this, MyService.class));
            startService(intent);
        }
   }
   private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}

MyService.class

public class MyService extends Service {
    TextView textDateTime;
    TextView textcurrentDateTime;
    DatabaseReference mDatabase;

    public MyService() {

    }

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

    public void onDestroy() {
        Log.d("destroy", "onDestroy");
    }

    @Override
    public void onStart(Intent intent, int startid) {
        //return super.onStartCommand(intent, flags, startId);
        String currentDate = DateFormat.getDateTimeInstance().format(new Date());
        mDatabase = FirebaseDatabase.getInstance().getReference();
        textcurrentDateTime.setText(currentDate);

        final Handler handler=new Handler();
        final Runnable updateTask=new Runnable() {
            @Override
            public void run() {
                updateCurrentTimeToFirebase();
                handler.postDelayed(this,7000);
                handler.removeCallbacks(this);
            }
        };

        handler.postDelayed(updateTask,7000);
    }
    public  void updateCurrentTimeToFirebase() {
        String currentDate = DateFormat.getDateTimeInstance().format(new Date());
        String id = mDatabase.push().getKey();
        mDatabase.child("autoUpdate").child(id).child("updateTime").setValue(currentDate);
    }
}
Sushin Pv
  • 1,826
  • 3
  • 22
  • 36