在Android中销毁服务时释放后调用的方法

时间:2016-05-08 09:44:53

标签: android android-camera android-service

我正在处理小型应用,需要在来电时闪烁闪光灯。我写了一个广播接收器,通过它我可以调用一个闪烁闪烁的服务。但是,当手机状态发生变化时,我会通过接收器调用stopService代码。 它在销毁服务时给我错误。

java.lang.RuntimeException: Unable to stop service aaro.flashalertoncall.android.BroadcastService@41b74638: java.lang.RuntimeException: Method called after release()

这是我的服务代码。



package aaro.flashalertoncall.android;

import android.annotation.TargetApi;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.IBinder;

import com.crashlytics.android.Crashlytics;


public class BroadcastService extends Service {

    static Camera cam = null;
    static int num = 0;
    Camera.Parameters cParams;
    boolean isLighOn = false;
    int sleepMS = 0;
    CameraManager cManager;
    private boolean bNewAPi = false;
    private Thread mThread;

    public BroadcastService() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        SharedPreferences pref = getApplicationContext().getSharedPreferences("xAAu07aVIyhSHvpKARuv", Context.MODE_PRIVATE);
        bNewAPi = pref.getBoolean("NewAPI", false);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        SharedPreferences pref = getApplicationContext().getSharedPreferences("xAAu07aVIyhSHvpKARuv", MODE_PRIVATE);


        final int iCallNotificationSpeed = pref.getInt("NotificationSpeedCall", 10);

        sleepMS = (2 / iCallNotificationSpeed == 0 ? 1 : iCallNotificationSpeed) * 50;

        mThread = new Thread() {
            @Override
            public void run() {
                try {
                    if (!bNewAPi) {
                        releaseCameraAndPreview();
                        cam = Camera.open();
                        cParams = cam.getParameters();
                        cam.setPreviewTexture(new SurfaceTexture(0));
                        cam.startPreview();

                        while (true) {
                            flipFlash();
                            Thread.sleep(sleepMS);
                            flipFlash();
                            Thread.sleep(sleepMS);
                        }
                    } else {
                        cManager = (CameraManager) getApplicationContext().getSystemService(getApplicationContext().CAMERA_SERVICE);
                        while (true) {
                            flipFlashNew();
                            Thread.sleep(sleepMS);
                            flipFlashNew();
                            Thread.sleep(sleepMS);
                        }
                    }
                } catch (Exception ex) {
                    Crashlytics.logException(ex);
                    stopSelf();
                }
            }
        };
        mThread.start();

        return START_NOT_STICKY;
    }

    private void releaseCameraAndPreview() {
        if (cam != null) {
            cam.stopPreview();
            cam.setPreviewCallback(null);
            cam.release();
        }
    }



    private void flipFlash() {
        try {
            if (isLighOn) {
                if (cParams != null) {
                    cParams.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                }
                cam.setParameters(cParams);
                isLighOn = false;
            } else {
                if (cParams != null) {
                    cParams.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                }
                cam.setParameters(cParams);
                isLighOn = true;
            }
        } catch (Exception ex) {
            Crashlytics.logException(ex);
        }
    }

    @TargetApi(Build.VERSION_CODES.M)
    private void flipFlashNew() throws CameraAccessException {
        try {
            if (isLighOn) {
                cManager.setTorchMode(cManager.getCameraIdList()[0], false);
                isLighOn = false;
            } else {
                cManager.setTorchMode(cManager.getCameraIdList()[0], true);
                isLighOn = true;
            }
        } catch (Exception ex) {
            Crashlytics.logException(ex);
        }
    }

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void onDestroy() {
        super.onDestroy();
        stopThread(mThread);

        if (!bNewAPi) {
            if (cam != null) {
                cam.stopPreview();
                cam.setPreviewCallback(null);
                cam.release();
            }
        } else {
            try {
                if (cManager != null) {

                    cManager.setTorchMode(cManager.getCameraIdList()[0], false);
                    cManager = null;
                }
            } catch (Exception ex) {
                Crashlytics.logException(ex);
            }
        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private synchronized void stopThread(Thread thread){
        if(thread != null) {
            thread = null;
        }
    }
}




请帮帮我。

0 个答案:

没有答案