尝试通过服务

时间:2015-11-27 12:02:06

标签: android service android-camera

我正在尝试通过相机在后台服务中捕获图像,但在执行此操作时,我的应用程序正在被绞死,有时它会给我错误的"无法连接到相机服务"虽然我的应用程序每隔20秒我的图像就会被存储,但要么被绞死,要么崩溃。

请查看我的服务类: -

public void onStart(Intent intent, int startId) {

    super.onStart(intent, startId);
    captureImage();
}

private void captureImage() {
    int count = 0;
    while (true) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (IS_ACTIVITY_FINISHED) {
            count++;
            if (count == 20) {
                // Start Activity here
                Intent translucent = new Intent(getApplicationContext(),
                        HiddenCamera.class);
                translucent.putExtra("FLASH", "off");
                translucent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(translucent);
                IS_ACTIVITY_FINISHED = false;
                break;
            }
        }
    }
}

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

这是我的HiddenCamera课程: -

http://piratepad.net/ep/pad/view/ro.LgLDgdzewfJ/latest

我的清单文件: -

  <uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
 <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.INTERNET" />

<service android:name="com.example.services.CameraService" >
        </service>

请向我提供有关此过程的任何有用信息。

由于

1 个答案:

答案 0 :(得分:0)

最后我能够解决它: -

这是我的服务类代码: -

private void startCapturingImage() {
    Toast.makeText(getApplicationContext(), "InSide service class", 1000)
                    .show();
    mDownTimer = new CountDownTimer(20000, 1000) {
            @SuppressWarnings("deprecation")
            @Override
            public void onFinish() {
                    // count finished

                    if (IS_ACTIVITY_FINISHED) {
                            Toast.makeText(getApplicationContext(), "InSide on finished method class", 1000)
                            .show();
                            IS_ACTIVITY_FINISHED = false;
                            Intent translucent = new Intent(getApplicationContext(),
                                            HiddenCamera.class);
                            translucent.putExtra("FLASH", "off");
                            translucent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(translucent);

                            mDownTimer.start();
                    }else{
                            startCapturingImage();
                    }
            }

            @Override
            public void onTick(long millisUntilFinished) {
            }
    }.start();