Android 打开关闭闪光灯(里程碑2.1)
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <Button
- android:id="@+id/open"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/open" />
- <Button
- android:id="@+id/close"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/close" />
- </LinearLayout>
Activity代码:
- package com.android.flashlight;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- public class AndroidFlashLightActivity extends Activity {
- /** Called when the activity is first created. */
- private Button mBtnOpen,mBtnClose;
- private MyFlashLight myFlashLight;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mBtnOpen = (Button) findViewById(R.id.open);
- mBtnClose = (Button) findViewById(R.id.close);
- try {
- myFlashLight = new MyFlashLight();
- } catch (Exception e) {
- e.printStackTrace();
- }
- mBtnOpen.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View v) {
- if(myFlashLight.isEnabled() == false){
- myFlashLight.enable(true);
- }
- }
- });
- mBtnClose.setOnClickListener(new Button.OnClickListener(){
- @Override
- public void onClick(View v) {
- if(myFlashLight.isEnabled() == true){
- myFlashLight.enable(false);
- }
- }
- });
- }
- }
封装的方法:
- package com.android.flashlight;
- import java.lang.reflect.Method;
- import android.os.IBinder;
- public class MyFlashLight {
- private Object svc = null;
- private Method getFlashlightEnabled = null;
- private Method setFlashlightEnabled = null;
- @SuppressWarnings("unchecked")
- public MyFlashLight() throws Exception{
- try {
- // call ServiceManager.getService("hardware") to get an IBinder for the service.
- // this appears to be totally undocumented and not exposed in the SDK whatsoever.
- Class sm = Class.forName("android.os.ServiceManager");
- Object hwBinder = sm.getMethod("getService", String.class).invoke(null, "hardware");
- // get the hardware service stub. this seems to just get us one step closer to the proxy
- Class hwsstub = Class.forName("android.os.IHardwareService$Stub");
- Method asInterface = hwsstub.getMethod("asInterface", android.os.IBinder.class);
- svc = asInterface.invoke(null, (IBinder) hwBinder);
- // grab the class (android.os.IHardwareService$Stub$Proxy) so we can reflect on its methods
- Class proxy = svc.getClass();
- // save methods
- getFlashlightEnabled = proxy.getMethod("getFlashlightEnabled");
- setFlashlightEnabled = proxy.getMethod("setFlashlightEnabled", boolean.class);
- }
- catch(Exception e) {
- throw new Exception("LED could not be initialized");
- }
- }
- public boolean isEnabled() {
- try {
- return getFlashlightEnabled.invoke(svc).equals(true);
- }
- catch(Exception e) {
- return false;
- }
- }
- public void enable(boolean tf) {
- try {
- setFlashlightEnabled.invoke(svc, tf);
- }
- catch(Exception e) {}
- }
- }
Android 打开关闭闪光灯(里程碑2.1)相关推荐
- Android打开/关闭数据流量
这个是网上找的,经过自己整理.由于android并未公开这个类,因此如果要打开/关闭数据,需要通过反射 16/09/29注:5.0以上无效 MobileDataSwitcher.java import ...
- Android打开/关闭屏幕和设置系统屏幕亮度方法
在有一次处理屏幕的时候,我通过设置值发现怎么都实现不了. 控制屏幕的两种方法: 第一:通过获取PowerManager对象,利用反射.打开/关闭屏幕,上代码: /** * 关闭屏幕 ,其实是使系统休 ...
- android 打开屏幕,android打开关闭屏幕
打开和关闭手机屏幕方法: 1.关闭屏幕 //设备管理者 private DevicePolicyManager mDevicePolicyManager; //关屏组件 private Compone ...
- Android打开/关闭免提(SpeakerPhone)
//打开扬声器 public void OpenSpeaker() { try{ AudioManager audioManager = (AudioManager) mC ...
- Android 打开关闭硬件加速
尊重原创,原文出处:http://www.cnblogs.com/frydsh/archive/2012/10/23/2733581.html Android从3.0(API Level 11)开始, ...
- Android打开/关闭wifi命令
1.切换root权限 # adb root; adb remount 2.关闭wifi # adb shell svc wifi disable 3.打开wifi # adb shell svc wi ...
- android 打开闪光灯
在android中打开闪光灯的方法有两种,一种是获取硬件服务,通过反射的方式来操作闪光灯.另外一种是获得Camera对象,通过设置Camera的参数来操作闪光灯.一下是一个操作闪光灯的工具类:实现了两 ...
- Android 获取光线强弱,开启闪光灯,关闭闪光灯
1.获取光线强度 (1)实现传感器接口 implements SensorEventListener 实例化 private SensorManager mSensroMgr;//传感器管理类 mS ...
- Android WiFi 打开关闭流程
本文简单介绍下WiFi打开与关闭流程,参考源码Android P. 一.WiFi 开机自动打开流程 系统服务启动的时候会启动WifiService,在SystemService.PHASE_SYSTE ...
最新文章
- Unity 2D游戏开发教程之摄像头追踪功能
- (一)安装docker
- java 自己抛空指针异常_java Timetask 访问service 抛空指针异常解决方案
- UA MATH563 概率论的数学基础I 概率空间1 基本概念
- [转]python yield
- 假设检验在数据分析中的应用
- 5.4shell编程3
- SQL SERVER 2008筛选时报错 无法为该请求检索数据
- Mobx入门之四:自定义reactions,when, autorun
- Linux 环境 简单启动 Java程序
- 亲和数(220/284)
- 前端开发的模块化和组件化的定义,以及两者的关系?
- MySQL table 添加_「primarykey」MySQL中为table添加primary key的两种方法 - seo实验室
- Python关键字keyword
- 如何解决‘CommandNotFoundError: Your shell has not been properly configured to use ‘conda activate’错误
- Android Hanlder的理解
- java-接口之运动员教练员综合案例
- 机器学习排序算法:RankNet to LambdaRank to LambdaMART
- Ewebeditor的问题
- tl wdr5660虚拟服务器,TP-Link TL-WDR5600端口映射设置教程