您的位置:首页 > 移动开发 > Android开发

Android 打开关闭闪光灯(里程碑2.1)

2012-09-26 11:30 267 查看
不同的手机,开启闪光灯的方法不一样,这里以摩托罗拉里程碑的手机为例

main.xml:

[html]
view plaincopy

<?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代码:

[java]
view plaincopy

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);
}
}
});
}
}

封装的方法:

[java]
view plaincopy

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) {}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: