Reputation: 247
Currently I'm trying to prevent my service from being killed by application manager. Everytime I swipe the application, service is also killed.
What I have tried is 1. using START_STICKY
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d(TAG, "onStartCommand started");
if(!isRunning){
mythread.start();
isRunning = true;
}
return START_STICKY;
}
In this example, my thread is showing toast every five seconds.
I also created a Broadcast Receiver and gave a service separate process
<service android:name="com.example.asd.mainhub.GPSTracker"
android:process=":my_process"
android:enabled="true" />
<receiver android:name="com.example.asd.mainhub.BootCompletedIntentReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="android.intent.action.PACKAGE_RESTARTED" />
<action android:name="com.example.asd.mainhub.BootCompletedIntentReceiver" />
</intent-filter>
</receiver>
onDestroy() method for service, which will use broadcast receiver to restore my service, didn't work.
public void onDestroy() {
Intent intent = new Intent("com.example.asd.mainhub.BootCompletedIntentReceiver");
intent.putExtra("yourvalue", "torestore");
sendBroadcast(intent);
}
MainActivity, calling my service onCreate()
gps = new GPSTracker();
mServiceIntent = new Intent(this, gps.getClass());
if (!isMyServiceRunning(gps.getClass())) {
startService(mServiceIntent);
}
I've also tried to use notification and foreground onCreate() of service:
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My Awesome App")
.setContentText("Doing some work...")
.setContentIntent(pendingIntent).build();
startForeground(1337, notification);
Still same, every time I swap my application, service is also dies, and no Toasts are shown anymore. In my console the last messages are: I/MAINACT: onDestroy! Application terminated. Seems like the service onDestroy() method is not even called. Please suggest something, how can i solve this.
P.S btw is I install app as a system app, will it solve the issue ?
EDIT1:
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// // Restore interrupt status.
// Thread.currentThread().interrupt();
// }
while(isRunning){
Log.d(TAG,"Running");
try {
postToastMessage("Toast from Service");
Thread.sleep(DELAY);
} catch (InterruptedException e) {
isRunning = false;
e.printStackTrace();
}
}
// Stop the service using the startId, so that we don't stop
// the service in the middle of handling another job
//stopSelf(msg.arg1);
}
}
public void postToastMessage(final String message) {
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}
});
}
static final long DELAY = 5000;
EDIT 2:
HelloService service = new HelloService();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this, service.getClass());
if (!isMyServiceRunning(service.getClass())) {
//startService(mServiceIntent);
startService(intent);
}
Upvotes: 1
Views: 2936
Reputation: 2203
What you're looking for is Services. You're most recommended to read the documention by Android Studio here.
Services will allow a very specific part of your application to survive at all times. Even when the user restart their devices and never run your application again. There is a lot to perceive about Services, but at the moment I believe a snippet will be most helpful to you, here is a little code,
First in your manifest
<!--notification-->
<service android:name="services.HelloService"
android:exported="false"
android:description="My notification Center"/>
<!--notification-->
Second
Create a class called HelloService
and paste the following code inside with the proper imports*
public class HelloService extends Service {
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
// Handler that receives messages from the thread
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// Restore interrupt status.
Thread.currentThread().interrupt();
}
// Stop the service using the startId, so that we don't stop
// the service in the middle of handling another job
stopSelf(msg.arg1);
}
}
@Override
public void onCreate() {
// Start up the thread running the service. Note that we create a
// separate thread because the service normally runs in the process's
// main thread, which we don't want to block. We also make it
// background priority so CPU-intensive work will not disrupt our UI.
HandlerThread thread = new HandlerThread("ServiceStartArguments",
Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "servicestarting",Toast.LENGTH_SHORT).show();
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
// If we get killed, after returning from here, restart
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
@Override
public void onDestroy() {
Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show();
}
}
Now, inside the 'handleMessage Method' add your bit of code, for example ;
@Override
public void handleMessage(Message msg) {
//... your bit of code goes here
try { Thread.sleep(5000);
// make a toast appears here
} catch (InterruptedException e) {
// Restore interrupt status.
Thread.currentThread().interrupt();
}
//stopSelf(msg.arg1); //... kill the service in your condition
}
Lastly, to activate the service class, in your function to trigger the service, first import the HelloService.class, then create an Intent and fire up!
Intent intent = new Intent(this, HelloService.class);
startActivity(intent)
Upvotes: -1
Reputation: 463
Use intent service instead of service, it runs on worker thread not main thread. So will keep running when application is removed from task manager.
Upvotes: 1