0

我正在开发一个手电筒应用程序,问题是该应用程序按预期工作,但在按下“home”或“back”键时却没有。我希望我的应用程序即使在用户按下“主页”键时也能保持手电筒灯亮起,以便用户可以在手电筒打开时做其他工作。在返回键上,闪光灯关闭很好。这是我的代码。请帮帮我,谢谢大家。

public class LightsOn extends Activity implements OnKeyListener
{
    ImageView button;
    Boolean flashon=true;
    private boolean hasFlash;
    Parameters myparas;
    private Camera mycamera;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_light);

        hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);

        button=(ImageView)findViewById(R.id.power);
        button.setBackgroundResource(R.drawable.offswitch);
        button.setOnClickListener(new View.OnClickListener() 
        {
            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub
                if(flashon)
                {
                    Toast.makeText(LightsOn.this, "Flashlight off..!!", Toast.LENGTH_SHORT).show();
                    button.setBackgroundResource(R.drawable.onswitch);
                    if (mycamera == null || myparas == null) 
                    {
                        return;
                    }
                    else
                    {
                        myparas = mycamera.getParameters();
                        myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
                        mycamera.setParameters(myparas);
                        mycamera.stopPreview();
                        flashon=false;
                    }
                }
                else
                {
                    Toast.makeText(LightsOn.this, "Flashlight on..!!", Toast.LENGTH_SHORT).show();
                    button.setBackgroundResource(R.drawable.offswitch);
                    if (mycamera == null || myparas == null) 
                    {
                        return;
                    }
                    else
                    {
                        myparas = mycamera.getParameters();
                        myparas.setFlashMode(Parameters.FLASH_MODE_TORCH);
                        mycamera.setParameters(myparas);
                        mycamera.startPreview();
                        flashon=true;
                    }
                }
            }
        });
    }

    private void getCamera() 
    {
        if (mycamera == null) 
        {
            try 
            {
                mycamera = Camera.open();
                myparas = mycamera.getParameters();
            } 
            catch (RuntimeException e)
            {
                Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
            }
        }

        if (!hasFlash)
        {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setTitle("Sorry your device doesnt support flash.");
            dialog.setMessage("Press 'OKAY' to exit..");
            dialog.setPositiveButton("Okay.. :( :(", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) 
                {
                    finish();
                    moveTaskToBack(true);
                }
            });
            dialog.setNegativeButton("More Apps by ****", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) 
                {
                    Toast.makeText(LightsOn.this, "Account Coming Soon..", Toast.LENGTH_SHORT).show();
                    finish();
                    moveTaskToBack(true);
                }
            });
            dialog.setNeutralButton("See website for more", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) 
                {
                    Toast.makeText(LightsOn.this, "Website to be Launched Soon..", Toast.LENGTH_SHORT).show();
                    finish();
                    moveTaskToBack(true);
                }
            });
        dialog.show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

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

        // on pause turn off the flash
        myparas = mycamera.getParameters();
        myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
        mycamera.setParameters(myparas);
        mycamera.stopPreview();
        flashon=false;
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

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

        if(hasFlash)
        myparas = mycamera.getParameters();
        myparas.setFlashMode(Parameters.FLASH_MODE_TORCH);
        mycamera.setParameters(myparas);
        mycamera.startPreview();
        flashon=true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        // on stop release the camera
        if (mycamera != null)
        {
            mycamera.release();
            mycamera = null;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        // on starting the app get the camera params
        getCamera();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu m)
    {
        MenuInflater inf=getMenuInflater(); 
        inf.inflate(R.menu.menu,m);
        return true;
    }
}
4

4 回答 4

2

得到解决方案。

您应该做的是,编写所有代码,如下所示onBackPressed。我将两种方法代码合并到一种方法中。

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    myparas = mycamera.getParameters();
    myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
    mycamera.setParameters(myparas);
    mycamera.stopPreview();
    flashon = false;

    if (mycamera != null) {
        mycamera.release();
        mycamera = null;
    }
    Log.d("Camera","Back Pressed");
}

onStop()并从和onPause()方法中删除所有代码。因为当您按下Home键时,它会先调用onPause(),然后再调用onStop()。我实现了您的代码并且对我来说工作得很好。

于 2013-06-18T06:29:30.210 回答
1

改变你onPause()这个,onStop()如果你需要

@Override
protected void onPause() {
    super.onPause();
}

并添加这个

@Override
protected void onBackPressed() { 
    super.onBackPressed();
    // on back turn off the flash
    myparas = mycamera.getParameters();
    myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
    mycamera.setParameters(myparas);
    mycamera.stopPreview();
    flashon=false;
}
于 2013-06-18T06:30:06.990 回答
1

明白了,答案就是简单的叠加onPause()方法,然后在onBackPressed()方法中关闭flash。

@Override
protected void onPause() {
super.onPause();
}

这是 onBackPressed()

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

myparas = mycamera.getParameters();
myparas.setFlashMode(Parameters.FLASH_MODE_OFF);
mycamera.setParameters(myparas);
mycamera.stopPreview();
flashon=false;
}

尽管现在我可以看到上面提供的答案也与 wel 相同。所以我猜现在它是一个经过充分验证的答案。@akshat 应该去争取它..

于 2013-06-18T07:23:01.607 回答
0

这里我修改了代码集!

它对我有用!谢谢

  @Override
protected void onDestroy() {
    super.onDestroy();
}

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

    // on pause turn off the flash
  //  turnOffFlash();
}

@Override
protected void onRestart() {
    super.onRestart();
}

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

    // on resume turn on the flash
    if(hasFlash)
        turnOnFlash();
}

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

    // on starting the app get the camera params
    getCamera();
}

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(params);
    camera.stopPreview();
    isFlashOn = false;

    if (camera != null) {
        camera.release();
        camera = null;
    }
    Log.d("Camera","Back Pressed");
}

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

    // on stop release the camera
 //   if (camera != null) {
 //       camera.release();
  //      camera = null;
   // }
}
于 2016-09-01T03:37:07.980 回答