Android 以编程方式打开手电筒按钮
Posted
技术标签:
【中文标题】Android 以编程方式打开手电筒按钮【英文标题】:Android Turn on flashlight programmatically button 【发布时间】:2014-10-28 18:01:04 【问题描述】:我正在开发一个应用程序,我需要能够通过按下按钮来打开 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>
Logcat:
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
任何关于我哪里出错的帮助或指导将不胜感激。另外,如果重要的话,我正在使用 Nexus 5 进行测试。我还从 playstore 下载了一个示例手电筒应用程序,效果很好,所以硬件很好。
提前感谢
【问题讨论】:
发布您的 Logcat。FlashLightBTN
是什么?
你在哪里初始化了FlashLightBTN
?
【参考方案1】:
在 camera.setParameters(p);
之后添加以下行在打开 Torch 时,您必须添加
camera.startPreview();
在关闭阶段你必须添加
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" />
希望对你有帮助。
【讨论】:
@Phoenix:这对你有帮助吗??请回复。 @Phoenix:只要去掉camera.release();并检查它是否工作? 我试过了,灯不亮,logcat也没有报错。 它在这两行给出了关于字符串库的错误:android:label="@string/permlab_flashlight" android:description="@string/permdesc_flashlight" 让我们continue this discussion in chat。【参考方案2】:看看这个源码,希望对你有帮助
https://github.com/survivingwithandroid/Swa-app/tree/master/TorchApp
【讨论】:
以上是关于Android 以编程方式打开手电筒按钮的主要内容,如果未能解决你的问题,请参考以下文章
有啥方法可以访问 Android 中的手电筒灯状态。我想以编程方式检查闪光灯是处于开启状态还是关闭状态