Android, How to make an application task open? Only closes by killing the task

I am developing an application that should always be running and should only be closed with a task killer or similar.

In the manifest:

android:persistent="true"

      

While the close does not close when I press the home button, from time to time I close it and I don't want this to happen.

I want a user, can close the application by killing it from a custom launcher or using task killer.

Any suggestions? thank you in advance

PD: how can you exit an application with the back button but not close it, I mean stop showing the application or any of its actions, but the application should continue to run in the background.

I wrote a service but something is not working, I added this to the manifest

<service android:name=".ONService"></service>

      

right before the app tag and this is ONService.java:

package com.omninotifier.main;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;

public class ONService extends Service{

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}


@Override
public void onCreate() {
    // TODO Auto-generated method stub
    //super.onCreate();
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    //return super.onStartCommand(intent, flags, startId);

    return START_STICKY;
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub

    NotificationManager manager = (NotificationManager)     
getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.ic_stat_alert,   
getResources().getString(R.string.ServiceDestroyed), System.currentTimeMillis());
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    manager.notify(150, notification);

    super.onDestroy();
}

 }

      

and I started the service doing it correctly when the app starts:

this is the main activity in the application

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


   Intent serviceIntent = new Intent();
   serviceIntent.setAction(".ONService");
   startService(serviceIntent);

//............

}

      

Do I have to add an intent filter with actions in the service tag for this to work? or is this another problem?

The problem was that it was unable to start the service. This is the fix:

in the manifest

<service android:name=".ONService">
<intent-filter android:priority="100">
    <action android:name=".ONService"/>
</intent-filter>
</service>

      

in the activity calling the service:

Intent serviceIntent = new Intent(".ONService");


startService(serviceIntent);

      

+2


source to share


5 answers


Write a service. The application communicates with the service. When your application exits, the service remains running.



http://developer.android.com/reference/android/app/Service.html

+1


source


I am developing an application that should always be running and should only be closed with a task killer or similar.

It's impossible. Android can and will eventually get rid of your process to prevent sloppy developers from trying "an app that should always be running".



If your app is part of a foreground user interface (like a music player), you can get a service that calls, which makes startForeground()

it less likely that Android will get rid of your process if it's Notification

in the status bar associated with your app. Your application will still terminate the process, although it will tend to increase.

Or you can create your own custom assembly of the Android operating system that contains the C daemon that implements your logic and distribute a modem ROM containing your customized Android.

+2


source


To keep your app running after it is closed, you need to make a service inside your app to do whatever you want in the background.

If you're new to Android app development, you might want to check out the following tutorial for Android Services: http://www.vogella.com/articles/AndroidServices/article.html . Also, I found the Android documentation to be very helpful. I would suggest you read both the API Guide for Services and the API Reference for the service class .

+1


source


Use a combination of Service, AlarmManager and BroadcastReceiver. The AlarmManager issues a pulse, the BCReceiver catches and starts the service whenever needed. This will essentially keep the service alive indefinitely as Android will see it being used periodically. Technically the lifecycle of your application, the service is under the control of androids, but you can start a sticky service that helps and calls it periodically. AlarmManager is extremely robust.

0


source


If you need to run all the time, check Service

and startForeground . If you can let your service die but reboot, take a look at onStartCommand and START_STICKY .

0


source







All Articles