I am not sure where/how I can run this service to work in background. I develop application which use a CountDownTimer. I found that I should create a CountDownTimer in Service.class and run a Service.class in MainActivity.class.
Why TextView doesn't show on my screen? I put this in OnCreate and onStartCommand. Next I try run this in MainActivity but without success.
I still try to implement my code to work this CountDownTimer properly in a background. On official Android website I see that they use a "Log.i" command to run this Foreground method. How can I use this in my code? Should I add this in Service.class?
Below is my code:
MainActivity:
Button btnStart, btnStop;
TextView textViewTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startService(View view)
{
Intent intent = new Intent(this,MyService.class);
startService(intent);
}
public void stopService(View view)
{
Intent intent = new Intent(this,MyService.class);
stopService(intent);
}
MyService:
Button btnStart, btnStop;
TextView textViewTime;
public String hms;
@Override
public void onCreate() {
btnStart = (Button) btnStart.findViewById(R.id.btnStart);
btnStop = (Button) btnStop.findViewById(R.id.btnStop);
textViewTime = (TextView) textViewTime.findViewById(R.id.textViewTime);
textViewTime.setText("00:01:30");
final CounterClass timer = new CounterClass(90000, 1000);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//startService(v);
timer.start();
}
});
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) { // create start a service
textViewTime.setText("00:01:30");
Toast.makeText(this, "Service Started...", Toast.LENGTH_LONG).show();
// timer.start();
//return START_STICKY; // return integer value
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() { // stop a service
Toast.makeText(this, "Service Destroyed...", Toast.LENGTH_LONG).show();
super.onDestroy();
}
@Nullable
@Override
public IBinder onBind(Intent intent) { // it's not needed but we must override this method
return null;
}
public class CounterClass extends CountDownTimer {
public CounterClass(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
long millis = millisUntilFinished;
hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(millis)),
TimeUnit.MILLISECONDS.toSeconds(millis) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis)));
System.out.println(hms);
textViewTime.setText(hms);
}
@Override
public void onFinish() {
textViewTime.setText("Completed.");
}
}
Firstly:
You have to write Broadcast Receiver in MainActivity.java and
register
it inoncreate()
andunregister
inonDetstroy()
.Secondly
In Service in
onTick()
method you have tosendbroadcast()
which will invokeonReceive()
in MainActivity.java and then in onReceive method you canchage text of Textview
of activity.Here is your solution was easy though :)
activity xml
Java code
service code
Forget to mention need to add service in manifest