1

我创建了一个 Torch 应用程序,授予手机硬件权限

并且它在三星安卓手机上工作,但在摩托罗拉手机上不工作(拒绝使用 2.3 安卓)

你能给我一些建议吗...

4

1 回答 1

0

这是我使用的代码

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.Camera.Parameters;
import android.content.pm.PackageManager;
import android.text.InputType;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ToggleButton;

public class MainActivity extends Activity implements View.OnClickListener{

ToggleButton tbutton;
//private boolean isLighOn = false;
//private Camera camera;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    flashtool();
    tbutton.setOnClickListener(this);
    Context context = this;
        PackageManager pm = context.getPackageManager();
        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
            /*Log.e("err", "Device has no camera!");
            // prepare the alert box                   
            AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

            // set the message to display
            alertbox.setMessage("This is the alertbox!");

            // add a neutral button to the alert box and assign a click listener
            alertbox.setNeutralButton("Ok", new DialogInterface.OnClickListener() {

                // click listener on the alert box
                public void onClick(DialogInterface arg0, int arg1) {
                    // the button was clicked
                    android.os.Process.killProcess(android.os.Process.myPid());
                    //this.finish();
                    finish();
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            });

            // show it
            alertbox.show();
            return;*/
        }
}

private void flashtool() {
    // TODO Auto-generated method stub
    tbutton = (ToggleButton)findViewById(R.id.tbutton);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.tbutton:

        if(tbutton.isChecked())
        {
            //Log.i("info", "torch is turn on!");
    /*      camera = camera.open();
            p = camera.getParameters();
            //Log.i("info", "torch is turn on!");

            p.setFlashMode(Parameters.FLASH_MODE_TORCH);

            camera.setParameters(p);
            camera.startPreview(); */
        //  isLighOn = true;  
            try {
            Camera cam = Camera.open();     
            Parameters p = cam.getParameters();
            p.setFlashMode(Parameters.FLASH_MODE_TORCH);
            cam.setParameters(p);
            cam.startPreview();
            }
            catch   (Exception e) {
                e.printStackTrace();

            }
        }
        else
        {

        /*  Log.i("info", "torch is turn off!");
            p = camera.getParameters();
            p.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(p);
            camera.stopPreview(); */
        //isLighOn = false;
            try{    
                Camera cam = Camera.open();     
            Parameters p = cam.getParameters();
            p.setFlashMode(Parameters.FLASH_MODE_OFF);
            cam.setParameters(p);
            cam.stopPreview();
                cam.release();
                cam = null;
            }
            catch   (Exception e) {
                e.printStackTrace();

            }
        }
    break;
    }
    }
}
于 2013-03-05T10:13:02.427 回答