【问题标题】:Application getting hanged when try to call camera class multiple times through service尝试通过服务多次调用相机类时应用程序被挂起
【发布时间】:2015-11-27 12:02:06
【问题描述】:

我正在尝试通过后台服务中的摄像头捕获图像,但是在执行此操作时,我的应用程序被挂起,有时它会给我“无法连接到摄像头服务”的错误,尽管我的图像每 20 秒后就会被存储一次我的应用程序 ui 要么被挂起,要么崩溃。

请查看我的服务类:-

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>

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

谢谢

【问题讨论】:

    标签: android service android-camera


    【解决方案1】:

    我终于可以解决它了:-

    这是我的服务类代码:-

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-11
      相关资源
      最近更新 更多