Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

android - Background service is not working in Oreo

I want to run my app in background if I kill the app instance also. But after I kill my app the service also stops working. Here is my code please any one help me to solve my issue.

I followed this link for running in the background but it is not working if I remove the instance. Please can any one show me how to run a background service if the instance is removed also?

This is my MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ctx = this;
    setContentView(R.layout.activity_main);
    Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(MainActivity.this, ALARM_REQUEST_CODE, alarmIntent, 0);
    mSensorService = new SensorService(getCtx());
    mServiceIntent = new Intent(getCtx(), mSensorService.getClass());
    if (!isMyServiceRunning(mSensorService.getClass())) {
        startService(mServiceIntent);
    }
}

Ths is my service class

   public class SensorService extends Service{

public int counter=0;
public SensorService(Context applicationContext) {
    super();
    Log.i("HERE", "here I am!");
}

public SensorService() {
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    startTimer();
    return START_STICKY;
}
@Override
public void onDestroy() {
    super.onDestroy();
    Log.i("EXIT", "ondestroy!");
    Intent broadcastIntent = new Intent("uk.ac.shef.oak.ActivityRecognition.RestartSensor");
    sendBroadcast(broadcastIntent);
  }

private Timer timer;
private TimerTask timerTask;
long oldTime=0;
public void startTimer() {
    //set a new Timer
    timer = new Timer();

    //initialize the TimerTask's job
    initializeTimerTask();

    //schedule the timer, to wake up every 1 second
    timer.schedule(timerTask, 1000, 1000); //
}

/**
 * it sets the timer to print the counter every x seconds
 */
public void initializeTimerTask() {
    timerTask = new TimerTask() {
        public void run() {
            Log.i("in timer", "in timer ++++  "+ (counter++));
        }
    };
}

/**
 * not needed
 */


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

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Its a long story. I have gone through it. Still implemented it. Now my service runs on every boot_complete event and keeps running all the time ( with a notification ).

  • Official Documentation:

Big NO. Google android developer documentation is poor, with no proper sample example too. It is theoretical and just theoretical. Keep reading if interested

https://developer.android.com/about/versions/oreo/background

Synopsis 1: You can only receive BOOT_COMPLETE and only few broadcasts in traditional receiver. Rest all broadcast receivers you need to implement runtime in a service by registering them through a code from service which always runs.

Synopsis 2: Again, you can not have always running processes in or above 8.0 (Oreo)... To achieve always running process... Create a Intentservice with proper notification of type ongoing and make OnStartCommand START_STICKY and register receiver with code in OnCreate

How to implement it : I have implemented it take reference from here : Oreo: Broadcast receiver Not working

Now Your Question : I want to run my app in background if it kills the app instance also.

With the help of above implementation link of my own you can achieve it

*Terms and conditions

You device must have proper android operating system released and burnt as it is.

Yes, I am using android :

No... You are Using Funtouch OS : VIVO ( By modifying Android)

There are many devices in market COLOR OS : OPPO ( By modifying Android) .... ....

  1. Already google has made it complicated... version by version....
  2. With no proper documentation and sample codes....
  3. And Now Independent mobile device manufacturers making a lot of changes to allow only selective applications run in background like WhatsApp, Facebook, Google Twitter Instagram

Now you will ask a developer question If these app runs in background then I can make my app run in background too....

No... They are OS based modifications to check if a service is from allowed vendors then only it can be alive there in background. If they will not allow these vendors then no one take phones which does not run these famous social apps.

Hushhhhhhhh.......


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...