You need to study the service first
Here is an example service
Create a new class and name it for Exmaple: MyService
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
return Null;
}
@Override
public void onCreate() {
Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();
}
@Override
public void onStart(Intent intent, int startId) {
Toast.makeText(this, " Service Started", Toast.LENGTH_LONG).show();
}
@Override
public void onDestroy() {
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}
Now in your main action you can start the service through this code
startService(new Intent(this, MyService.class));
To stop the service, put this code in MainActivity
stopService(new Intent(this, MyService.class));
See this post
.
http://www.javacodegeeks.com/2014/01/android-service-tutorial.html
http://examples.javacodegeeks.com/android/core/service/android-service-example/
EDIT:
: Activity Messaging
http://www.intertech.com/Blog/using-localbroadcastmanager-in-service-to-activity-communications/