Can I Make A Background Service That runs at certain intervals of time even after closing the Application

烈酒焚心 提交于 2019-12-11 14:43:33

问题


I am Referring this Application https://github.com/hzitoun/android-camera2-secret-picture-taker. In this Application there are two classes(APictureCapturingService.java & PictureCapturingServiceImpl.java) that takes pictures without preview can these two classes be converted to Background Service that runs always never dies.

Is this possible to have camera capturing process as a background service if yes how to proceed?


回答1:


I don't know how you are taking picture in your activity but i can guide you to run your app in background even your close your app and you can able to run your camera code in every second..

public class SampleService extends Service {

public SampleService() {
}


@Override
public IBinder onBind(Intent intent) {
    // TODO: Return the communication channel to the service.
    return null;
}

@Override
public int onStartCommand(final Intent inten, int flags, int startId) {

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {

            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {


                        // Make use your method here..

                }
            });
        }
    }, 5000, 2000);

    return super.onStartCommand(inten, flags, startId);
}



@Override
public void onCreate() {
    super.onCreate();
}}

And you need to start the service form activity..

startService(new Intent(this, SampleService.class));

I have used this for monitoring the foreground app it will work..



来源:https://stackoverflow.com/questions/47151213/can-i-make-a-background-service-that-runs-at-certain-intervals-of-time-even-afte

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!