我已按照this指南创建了一个简单的手电筒应用。我从应用程序中删除了一些功能(图像更改,声音),试图删除阻止手电筒进入的任何问题。
我正在使用运行4.4.2的Nexus 5
主要活动:
package com.test.testFlashlight;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class testFlashlight extends Activity {
Button btnSwitch;
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_testFlashlight);
// flash switch button
btnSwitch = (Button) findViewById(R.id.btnFlash);
// First check if device is supporting flashlight or not
hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
// device doesn't support flash
// Show alert message and close the application
AlertDialog alert = new AlertDialog.Builder(testFlashlight.this)
.create();
alert.setTitle("Error");
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// closing the application
finish();
}
});
alert.show();
return;
}
// get the camera
getCamera();
// displaying button image
//toggleButtonImage();
// Switch button click event to toggle flash on/off
btnSwitch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Debug_point_1", "Flashlight evaluation if on");
if (isFlashOn) {
// turn off flash
// closing the application
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
}
// Get the camera
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
// Turning On flash
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
//playSound();
Log.d("Debug_point_2", "Turning flashlight on");
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
Log.d("Debug_point_3", "Flashlight should be on");
// changing button/switch image
//toggleButtonImage();
}
}
// Turning Off flash
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
playSound();
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
// changing button/switch image
//toggleButtonImage();
}
}
// Playing sound
// will play button toggle sound on flash on / off
private void playSound(){/*
if(isFlashOn){
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
}else{
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();*/
}
/*
* Toggle switch button images
* changing image states to on / off
* */
private void toggleButtonImage(){
}
/*
if(isFlashOn){
btnSwitch.setImageResource(R.drawable.btn_switch_on);
}else{
btnSwitch.setImageResource(R.drawable.btn_switch_off);
}*/
@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
protected void onStop() {
super.onStop();
// on stop release the camera
if (camera != null) {
camera.release();
camera = null;
}
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.testFlashlight" >
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.test.testFlashlight.testFlashlight"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_testFlashlight.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.testFlashlight.testFlashlight"
tools:ignore="MergeRootFrame">
</FrameLayout>
fragment_testFlashlight.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.test.testFlashlight.testFlashlight$PlaceholderFragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Flashlight"
android:id="@+id/btnFlash"
android:clickable="false"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignParentEnd="false" />
</RelativeLayout>
Logcat部分我认为很重要:
03-31 19:22:13.146 784-1192/? E/LocSvc_IzatApiV02? W/virtual int izat_core::IzatApiV02::injectLocation(GpsExtLocation):665]: error! inject position failed
03-31 19:22:13.156 784-1192/? E/LocSvc_ApiV02? W/virtual loc_api_adapter_err LocApiV02::injectPosition(double, double, float):492]: error! status = eLOC_CLIENT_FAILURE_INVALID_PARAMETER, inject_pos_ind.status = UNKNOWN
03-31 19:22:13.806 184-1036/? I/AwesomePlayer? setDataSource_l(URL suppressed)
03-31 19:22:13.816 184-837/? D/audio_hw_primary? select_devices: out_snd_device(2: speaker) in_snd_device(0: )
03-31 19:22:13.846 784-865/? D/ConnectivityService? Sampling interval elapsed, updating statistics ..
03-31 19:22:13.866 1041-1041/? D/PhoneStatusBar? disable: < expand* icons alerts ticker* system_info back* home* recent* clock* search >
03-31 19:22:13.886 784-865/? D/ConnectivityService? Done.
03-31 19:22:13.886 784-865/? D/ConnectivityService? Setting timer for 720seconds
03-31 19:22:13.906 184-1036/? I/AwesomePlayer? setDataSource_l(URL suppressed)
03-31 19:22:13.906 2064-2064/? D/YouTube? apps.youtube.app.prefetch.f.onReceive:366 Received: android.intent.action.USER_PRESENT
03-31 19:22:13.906 1244-2943/? D/NfcService? NFC-C ON
03-31 19:22:13.916 1366-1370/? D/dalvikvm? GC_CONCURRENT freed 657K, 5% free 18682K/19584K, paused 3ms+2ms, total 42ms
03-31 19:22:13.916 1366-1489/? D/dalvikvm? WAIT_FOR_CONCURRENT_GC blocked 26ms
03-31 19:22:13.926 184-1036/? I/Camera2ClientBase? Camera 0: Opened
03-31 19:22:13.936 184-1036/? E/mm-camera-intf? mm_camera_open: dev name = /dev/video1, cam_idx = 1
03-31 19:22:13.936 198-198/? E/mm-camera-sensor? module_sensor_start_session:577 session 1
03-31 19:22:13.936 784-1192/? E/LocSvc_IzatApiV02? W/virtual int izat_core::IzatApiV02::injectLocation(GpsExtLocation):665]: error! inject position failed
03-31 19:22:13.946 198-198/? E/mm-camera-sensor? eeprom_load_library:515 e_ctrl->eeprom_lib.func_tbl =0xb6b67004
03-31 19:22:13.946 784-1192/? E/LocSvc_ApiV02? W/virtual loc_api_adapter_err LocApiV02::injectPosition(double, double, float):492]: error! status = eLOC_CLIENT_FAILURE_INVALID_PARAMETER, inject_pos_ind.status = UNKNOWN
03-31 19:22:13.976 198-198/? I/mm-camera? gyro_module_start_session: Enter
03-31 19:22:13.976 198-198/? I/mm-camera? gyro_module_start_session: Init DSPS
03-31 19:22:13.976 198-198/? I/mm-camera? gyro_module_start_session: Exit successful
03-31 19:22:13.976 198-198/? I/mm-camera? gyro_module_get_port: Exit successful
03-31 19:22:13.976 198-198/? E/mm-camera? cpp_module_start_session:352, info: starting session 1
03-31 19:22:13.996 198-198/? E/mm-camera? cpp_module_start_session:422, info: cpp_thread created.
03-31 19:22:13.996 198-11446/? E/mm-camera? cpp_thread_func:55: cpp_thread entering the polling loop...
03-31 19:22:13.996 198-198/? E/mm-camera? cpp_module_start_session:425, info: session 1 started.
03-31 19:22:13.996 198-198/? E/mm-camera? c2d_module_start_session:246, info: starting session 1
03-31 19:22:13.996 198-11447/? E/mm-camera? c2d_thread_func:39: c2d_thread entering the polling loop...
03-31 19:22:13.996 198-198/? E/mm-camera? c2d_module_start_session:284, info: c2d_thread created.
03-31 19:22:13.996 198-198/? E/mm-camera? c2d_module_start_session:306, info: session 1 started.
03-31 19:22:13.996 198-198/? E/mm-camera-sensor? module_module_set_session_data:2435 max delay 2 report dSelay 1
03-31 19:22:13.996 198-198/? E/mm-camera? module_faceproc_set_session_data:1826] Per frame control 2 1
03-31 19:22:13.996 184-1036/? E/mm-camera-intf? mm_camera_open: opened, break out while loop
03-31 19:22:13.996 184-1036/? I/Camera2-Parameters? Camera 0: Disabling ZSL mode
03-31 19:22:13.996 10556-10556/com.test.testFlashlight D/Debug_point_2? Turning flashlight on
03-31 19:22:14.006 184-3785/? E/QCamera3HWI? camera_metadata_t* qcamera::QCamera3HardwareInterface::translateCapabilityToMetadata(int): Setting focus mode to auto
03-31 19:22:14.006 184-3785/? E/QCamera3HWI? camera_metadata_t* qcamera::QCamera3HardwareInterface::translateCapabilityToMetadata(int): Setting focus mode to auto
03-31 19:22:14.006 10556-10556/com.test.testFlashlight D/Debug_point_3? Flashlight should be on
03-31 19:22:14.036 10556-10556/com.test.testFlashlight D/dalvikvm? GC_FOR_ALLOC freed 281K, 2% free 16922K/17232K, paused 19ms, total 19ms
03-31 19:22:14.086 1168-1168/? I/WallpaperService? engine paused
03-31 19:22:14.086 1168-1168/? I/AndroidInput? sensor listener tear down
我想知道在哪里需要关注以允许激活闪光灯。
答案 0 :(得分:0)
你可以把它放在你的Manifest中:
<uses-feature android:name="android.hardware.camera.flash" />
对于某些手机,您需要使用SurfaceView来获取闪光灯。此外,最好使用:
List<String> flashModes = parameters.getSupportedFlashModes();
因为那里有各种各样的设备。手电筒并不好玩,我的朋友。