Android以编程方式打开手电筒按钮

Android以编程方式打开手电筒按钮,android,android-fragments,android-camera,flashlight,Android,Android Fragments,Android Camera,Flashlight,我正在开发一个应用程序,在这个应用程序中,我需要能够通过按下按钮打开Android手机的手电筒。我写了一些代码。但是,按下按钮后,指示灯不亮。以下是相关代码。请注意,这是在一个片段中完成的。我尝试了一个有同样问题的独立应用程序 public class Flashlight extends Fragment implements View.OnClickListener { // in global private Camera camera; private Butto

我正在开发一个应用程序,在这个应用程序中,我需要能够通过按下按钮打开Android手机的手电筒。我写了一些代码。但是,按下按钮后,指示灯不亮。以下是相关代码。请注意,这是在一个片段中完成的。我尝试了一个有同样问题的独立应用程序

public class Flashlight extends Fragment implements View.OnClickListener {
    // in global
    private Camera camera;
    private Button button;
    Button FlashLightBTN
    // end global

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.udp_commands, container, false);

          FlashLightBTN = (Button) rootView.findViewById(R.id.flashlightBTN);

        Context context = getActivity().getApplicationContext();
        PackageManager pm = context.getPackageManager();
        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
            // Log.e("err", "Device has no camera!");
            Toast.makeText(getActivity().getApplicationContext(), "Your device doesn't have camera!", Toast.LENGTH_SHORT).show();

            return;
        }
        camera = Camera.open();
        final Parameters p = camera.getParameters();
        FlashLightBTN.setTag(1);
        FlashLightBTN.setText("Turn Flashlight On");
        FlashLightBTN.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                Button b = (Button) v;
                String buttonText = b.getText().toString();
                final int status = (Integer) v.getTag();

                if (status == 1) {
                    p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                   camera.setParameters(p);
                camera.startPreview();

                FlashLightBTN.setText("Turn Flashlight Off");
                v.setTag(0);
            }
            if (status == 0) {
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(p);
                camera.stopPreview();
                camera.release();                        FlashLightBTN.setText("Turn Flashlight On");
                    v.setTag(1);
                }

            }
        });
        // other code
        return rootView;
    }
}
下面是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.andy_xmark1"
    android:versionCode="1"
    android:versionName="1.0"
    android:windowSoftInputMode="stateHidden" >

    <!-- Allows access to the flashlight -->
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera.flash" />
    <uses-feature android:name="android.hardware.camera" />

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.flashlight.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
日志:

09-04 00:50:25.279: I/Adreno-EGL(11871): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM Build: I0404c4692afb8623f95c43aeb6d5e13ed4b30ddbDate: 11/06/13
09-04 00:50:25.299: D/OpenGLRenderer(11871): Enabling debug mode 0
09-04 00:50:25.329: D/dalvikvm(11871): GC_FOR_ALLOC freed 218K, 2% free 16986K/17236K, paused 11ms, total 11ms
09-04 00:50:28.909: D/dalvikvm(11871): GC_FOR_ALLOC freed 387K, 3% free 17113K/17532K, paused 14ms, total 14ms
09-04 00:50:33.659: D/dalvikvm(11871): GC_FOR_ALLOC freed 510K, 4% free 17117K/17660K, paused 15ms, total 15ms
09-04 00:50:35.999: D/AndroidRuntime(11871): Shutting down VM
09-04 00:50:35.999: W/dalvikvm(11871): threadid=1: thread exiting with uncaught exception (group=0x41cf2ba8)
09-04 00:50:36.009: E/AndroidRuntime(11871): FATAL EXCEPTION: main
09-04 00:50:36.009: E/AndroidRuntime(11871): Process: com.example.flashlight, PID: 11871
09-04 00:50:36.009: E/AndroidRuntime(11871): java.lang.RuntimeException: Method called after release()
09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.hardware.Camera.native_setParameters(Native Method)
09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.hardware.Camera.setParameters(Camera.java:1650)
09-04 00:50:36.009: E/AndroidRuntime(11871):    at com.example.flashlight.flash$11.onClick(UDP_Commands.java:225)
09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.view.View.performClick(View.java:4438)
09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.view.View$PerformClick.run(View.java:18422)
09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.os.Handler.handleCallback(Handler.java:733)
09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.os.Handler.dispatchMessage(Handler.java:95)
09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.os.Looper.loop(Looper.java:136)
09-04 00:50:36.009: E/AndroidRuntime(11871):    at android.app.ActivityThread.main(ActivityThread.java:5017)
09-04 00:50:36.009: E/AndroidRuntime(11871):    at java.lang.reflect.Method.invokeNative(Native Method)
09-04 00:50:36.009: E/AndroidRuntime(11871):    at java.lang.reflect.Method.invoke(Method.java:515)
09-04 00:50:36.009: E/AndroidRuntime(11871):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-04 00:50:36.009: E/AndroidRuntime(11871):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-04 00:50:36.009: E/AndroidRuntime(11871):    at dalvik.system.NativeStart.main(Native Method)
09-04 00:50:37.869: I/Process(11871): Sending signal. PID: 11871 SIG: 9
如果您能为我提供帮助或指导,我将不胜感激。如果这很重要的话,我正在用Nexus5测试它。我还从playstore下载了一个示例手电筒应用程序,效果很好,所以硬件很好用


提前感谢

在camera.setParametersp之后添加以下行

在打开手电筒时,您必须添加

在关闭阶段,您必须添加

用于打开/关闭手电筒:

添加以下权限:-

<!-- Allows access to the flashlight -->
 <permission android:name="android.permission.FLASHLIGHT"
             android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
             android:protectionLevel="normal"
             android:label="@string/permlab_flashlight"
             android:description="@string/permdesc_flashlight" />

希望它能帮助您。

看看这个源代码,我希望它能帮助您


张贴日志。什么是FlashLightBTN?您在哪里初始化了FlashLightBTN?@Phoenix:这对urs有帮助吗???。请回答。@Phoenix:只需删除线camera.release;检查它是否工作?我试过了,灯不亮,logcat没有报告错误。它在这两行给出了一个关于字符串库的错误:android:label=@string/permlab\u flashlight android:description=@string/permdesc\u flashlightLet us。
camera.stopPreview();
camera.release();
<!-- Allows access to the flashlight -->
 <permission android:name="android.permission.FLASHLIGHT"
             android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
             android:protectionLevel="normal"
             android:label="@string/permlab_flashlight"
             android:description="@string/permdesc_flashlight" />