即使关闭应用程序后,我仍可以使后台服务在一定时间间隔运行吗? [英] Can I Make A Background Service That runs at certain intervals of time even after closing the Application

查看:188
本文介绍了即使关闭应用程序后,我仍可以使后台服务在一定时间间隔运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在引用此应用程序 https://github.com/hzitoun/android-camera2-secret-picture-taker . 在此应用程序中,有两个类(APictureCapturingService.java和PictureCapturingServiceImpl.java)可以在不预览的情况下拍照,可以将这两个类转换为永远不会消失的后台服务.

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?

推荐答案

我不知道您如何在活动中拍照,但是即使您关闭应用程序,我也可以指导您在后台运行您的应用程序,并且您可以每秒运行一次相机代码.

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..

这篇关于即使关闭应用程序后,我仍可以使后台服务在一定时间间隔运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆