简体   繁体   中英

Android Service not running in background?

The service runs when I press home button or back button but the service is not running in background when app is closed. Moreover the service runs in background in some(LG Nexus 5) phones, but in most of the phones(Samsung,Xioami) service is not running when app is closed. When i goto Settings > Apps > Running, It always shows as app running and displays 1 service 1 thread. I am calling the service from MainActivity.java

I want the service always running in background even if the app is closed. Any help will be appreciated.

Here is code for service TimeService.java

public class TimeService extends Service implements
        ConnectionCallbacks, OnConnectionFailedListener {
    // constant
    public static final long NOTIFY_INTERVAL =  30 * 60 * 1000; // 30 minutes

// run on another Thread to avoid crash
private Handler mHandler = new Handler();
// timer handling
private Timer mTimer = null;

@Override
public void onCreate() {
    // cancel if already existed
    if (mTimer != null) {
        mTimer.cancel();
    } else {
        // recreate new
        mTimer = new Timer();
    }
    // schedule task
    mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL);
}

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


class TimeDisplayTimerTask extends TimerTask {


    @Override
    public void run() {
        // run on another thread
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                // Send message in background

               sendSMS(number,msg)

                               }
                         });
                 }
             }

MainActivity.java

public class MainActivity extends Activity
{
    Button btnIn;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    btnIn = (Button) findViewById(R.id.btnIn);

    btnIn.setOnClickListener(new View.OnClickListener() {

        boolean enable = true;
        @Override
        public void onClick(View v) {

                Intent intent = new Intent(MainActivity.this, TimeService.class);
                   startService(intent);
}
}
}

Android manifest

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

You should use a foreground service if you want keep alive your service.

To make it you must run the service in foreground

private void runForeground(){
//... Pending intent if you want attach it to the notification


Notification notification=new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .setContentText(getString(R.string.string))
                            .setContentIntent(pendingIntent).build();

startForeground(NOTIFICATION_ID, notification);

}

NOTIFICATION_ID is a number for identify it, return sticky the service.

I was running the background task in a separate thread, while it should be in the main thread, that was the reason app was not running in background in some Phones.

class TimeDisplayTimerTask extends TimerTask {
@Override
public void run() {       
            // Send message in background
           sendSMS(number,msg);
             }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM