前言:我把我的安卓手机恢复出厂设置以后,手电筒这个软件莫名消失了,想要从应用市场下载一个使用,发现都有广告,那么只好手写一个了。
点击下方链接下载手电筒app:
https://download.csdn.net/download/sinat_30949835/12630516
源码分享:
MainActivity.java
package com.lnkjly.shoudiantong;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCharacteristics;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button button;
private CameraManager mCameraManager;
private boolean isFlashLightOn = false;
@TargetApi(Build.VERSION_CODES.M)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.btn_sdt);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toggleLight(!isFlashLightOn);
}
});
//获取CameraManager
if(mCameraManager == null){
mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
}
mCameraManager.registerTorchCallback(mTorchCallback, null);
}
@TargetApi(Build.VERSION_CODES.M)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private CameraManager.TorchCallback mTorchCallback = new CameraManager.TorchCallback() {
@Override
public void onTorchModeChanged(@NonNull String cameraId, boolean enabled) {
super.onTorchModeChanged(cameraId, enabled);
// Log.Loge(TAG, "onTorchModeChanged cameraId=" + cameraId + ";enabled=" + enabled);
isFlashLightOn = enabled;
// ivFlashlight.setIcon(enabled ? MaterialDrawableBuilder.IconValue.FLASHLIGHT : MaterialDrawableBuilder.IconValue.FLASHLIGHT_OFF);
}
};
@TargetApi(Build.VERSION_CODES.M)
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public void toggleLight(boolean OPEN) {
try {
//获取当前手机所有摄像头设备ID
String[] ids = mCameraManager.getCameraIdList();
for (String id : ids) {
CameraCharacteristics c = mCameraManager.getCameraCharacteristics(id);
//查询该摄像头组件是否包含闪光灯
Boolean flashAvailable = c.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
Integer lensFacing = c.get(CameraCharacteristics.LENS_FACING);
if (flashAvailable != null && flashAvailable
&& lensFacing != null && lensFacing == CameraCharacteristics.LENS_FACING_BACK) {
//打开或关闭手电筒
mCameraManager.setTorchMode(id, OPEN);
break;
}
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
protected void onDestroy() {
super.onDestroy();
if(mCameraManager != null && mTorchCallback != null){
mCameraManager.unregisterTorchCallback(mTorchCallback);
}
}
}
app/src/main/res/layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<View
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/btn_sdt"
android:layout_width="300dp"
android:layout_height="300dp"
android:text="手电筒"
android:layout_marginBottom="50dp"
/>
</LinearLayout>
</LinearLayout>
build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "com.lnkjly.shoudiantong"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
logo的话随便找一个就可以了。
通过以上代码就可以完成最简单的一个手电筒应用哦。