1

我正在为 Nexus 和 Galaxy 设备开发 FlashLight 应用程序。该应用程序可以在 Galaxy 上运行,但有一件事出了问题。当我将应用程序设置为后台(通过按主页按钮)然后单击小部件时,它会从 mainactivity 打开 onResume ,就像您在此处看到的那样:

在此处输入图像描述

所以我的问题是:

  1. 如何检查活动(在本例中为 FlashLight.java)是否正在运行?
  2. 如何从 invisible_activity.java 中终止正在运行的活动?
  3. 为什么会调用 FlashLight.java 中的 onResume(),我该如何控制这个问题?

来自红场的 LogCat:

    11-18 15:36:03.300: D/Camera(9781): app passed NULL surface
11-18 15:36:03.400: D/InputTransport(9781): Input channel constructed: name='ClientState{4366a1d0 uid 10141 pid 9781} (server)', fd=56
11-18 15:36:03.445: D/AndroidRuntime(9781): Shutting down VM
11-18 15:36:03.445: W/dalvikvm(9781): threadid=1: thread exiting with uncaught exception (group=0x41d26700)
11-18 15:36:03.460: E/AndroidRuntime(9781): FATAL EXCEPTION: main
11-18 15:36:03.460: E/AndroidRuntime(9781): java.lang.RuntimeException: Unable to resume activity {spicysoftware.spicytaschenlampe/spicysoftware.spicytaschenlampe.FlashLight}: java.lang.RuntimeException: Fail to connect to camera service
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2918)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2947)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.os.Looper.loop(Looper.java:137)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.ActivityThread.main(ActivityThread.java:5415)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at java.lang.reflect.Method.invokeNative(Native Method)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at java.lang.reflect.Method.invoke(Method.java:525)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at dalvik.system.NativeStart.main(Native Method)
11-18 15:36:03.460: E/AndroidRuntime(9781): Caused by: java.lang.RuntimeException: Fail to connect to camera service
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.hardware.Camera.native_setup(Native Method)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.hardware.Camera.<init>(Camera.java:413)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.hardware.Camera.open(Camera.java:384)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at spicysoftware.spicytaschenlampe.FlashLight.check_for_flash(FlashLight.java:82)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at spicysoftware.spicytaschenlampe.FlashLight.onResume(FlashLight.java:74)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1209)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.Activity.performResume(Activity.java:5450)
11-18 15:36:03.460: E/AndroidRuntime(9781):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2908)
11-18 15:36:03.460: E/AndroidRuntime(9781):     ... 10 more

来自 FlashLight.java 的 onResume()

它在“cam = Camera.open();”处崩溃 当我单击小部件时。

@Override
protected void onResume(){
 super.onResume();

 check_settings();

 if(light_ == 0){
        finish();
        Intent i_screenlight = new Intent(FlashLight.this, ScreenLight_noFlash.class);  
        startActivity(i_screenlight); 
 }else{
 mSensorManager.registerListener(mSensorListener, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
    if(cam != null){

    } else {
        rLayout = (RelativeLayout) findViewById (R.id.layout_background);       
        cam = Camera.open();  //===> It's crashing here, when I open the widget     
        cam.startPreview();
        p = cam.getParameters();

        if(light == 1){     
            if(sound == 0){
                mPlayer.start();   
            }
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                cmd_flash_light.setText(R.string.off);
                cmd_flash_light.setBackgroundResource(R.drawable.ein_2);
                cam.setParameters(p);
                cam.startPreview();

        } else {

            if(sound == 0){
                mPlayer.start(); 
            }
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                cmd_flash_light.setText(R.string.on);
                cmd_flash_light.setBackgroundResource(R.drawable.ein);
                cam.setParameters(p);           
                cam.startPreview();
        }
    }
 }
}

接收器,调用 invisible_activity.java

@Override
public void onReceive(Context context, Intent intent) {

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.flash_widget);

        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
        appWidgetManager.updateAppWidget(new ComponentName(context,     FlashlightWidgetProvider.class),
                                                                         views);
        Intent i = new Intent();
        i.setClassName("spicysoftware.spicytaschenlampe", "spicysoftware.spicytaschenlampe.invisible_activity");
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);       
}

invisible_activity.java(用于处理 LED 灯的小部件)

public class invisible_activity extends Activity implements SurfaceHolder.Callback{
    private static Camera cam_;
    SurfaceView preview_;
    SurfaceHolder mHolder;
    shell_preferences shellpreference = new shell_preferences(this);
    int isLightOn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //Remove notification bar
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.invisible);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        isLightOn = shellpreference.getInt("LIGHT_ACT", 0);
         if (isLightOn == 1) {
             if (cam_ != null) {
                     cam_.stopPreview();
                     cam_.release();
                     cam_ = null;
                     finish();
             }
             shellpreference.setInt("LIGHT_ACT", 0);

     } else {
             // Open the default i.e. the first rear facing cam_.
             cam_ = Camera.open();

                     // Set the torch flash mode
                     Parameters param = cam_.getParameters();
                     param.setFlashMode(Parameters.FLASH_MODE_TORCH);

                    preview_ = (SurfaceView) findViewById(R.id.surfaceView2);
                    mHolder = preview_.getHolder();
                    mHolder.addCallback(this);

                    try {           
                        cam_.setPreviewDisplay(mHolder);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                        cam_.setParameters(param);
                        cam_.startPreview();
                             shellpreference.setInt("LIGHT_ACT", 1);
                                finish();
     }

    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub

    }

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        mHolder = holder;
        try {
            cam_.setPreviewDisplay(mHolder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        if(cam_ != null){
         cam_.stopPreview();
            mHolder = null;

        }
    }

}
4

1 回答 1

1

我认为当您实际销毁您的应用程序/活动时,您忘记了释放相机。

尝试覆盖 onDestroy() :

@Override
protected void onDestroy() {
    super.onDestroy();
    if (camera!=null)
    {
        camera.stopPreview();
        camera.release();
        camera=null;
   }
}

至少您的 logcat 指出了这个问题:

11-18 15:36:03.460: E/AndroidRuntime(9781): Caused by: java.lang.RuntimeException: 无法连接到相机服务

于 2013-11-18T16:20:56.903 回答