1个回答
展开全部
public class HandLightActivity extends Activity implements OnClickListener{
private ToggleButton toggleButton;
private Camera m_Camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.handlight);
toggleButton = (ToggleButton)this.findViewById(R.id.toggleButton1);
toggleButton.setOnClickListener(this);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@Override
public void onClick(View v) {
ToggleButton tb = (ToggleButton)v;
if(!tb.isChecked()){
PackageManager pm= this.getPackageManager();
FeatureInfo[] features=pm.getSystemAvailableFeatures();
for(FeatureInfo f : features)
{
if(PackageManager.FEATURE_CAMERA_FLASH.equals(f.name)) //判断设备是否支持闪光灯
{
if ( null == m_Camera )
{
m_Camera = Camera.open();
}
Camera.Parameters parameters = m_Camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
m_Camera.setParameters( parameters );
m_Camera.startPreview();
toggleButton.setBackgroundColor(0x30ffffff);
}
}
}else{
if ( m_Camera != null )
{
m_Camera.stopPreview();
m_Camera.release();
m_Camera = null;
}
toggleButton.setBackgroundColor(0xffffffff);
}
}
}
需要加上权限
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.HARDWARE_TEST" />
private ToggleButton toggleButton;
private Camera m_Camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.handlight);
toggleButton = (ToggleButton)this.findViewById(R.id.toggleButton1);
toggleButton.setOnClickListener(this);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@Override
public void onClick(View v) {
ToggleButton tb = (ToggleButton)v;
if(!tb.isChecked()){
PackageManager pm= this.getPackageManager();
FeatureInfo[] features=pm.getSystemAvailableFeatures();
for(FeatureInfo f : features)
{
if(PackageManager.FEATURE_CAMERA_FLASH.equals(f.name)) //判断设备是否支持闪光灯
{
if ( null == m_Camera )
{
m_Camera = Camera.open();
}
Camera.Parameters parameters = m_Camera.getParameters();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
m_Camera.setParameters( parameters );
m_Camera.startPreview();
toggleButton.setBackgroundColor(0x30ffffff);
}
}
}else{
if ( m_Camera != null )
{
m_Camera.stopPreview();
m_Camera.release();
m_Camera = null;
}
toggleButton.setBackgroundColor(0xffffffff);
}
}
}
需要加上权限
<uses-permission android:name="android.permission.FLASHLIGHT"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.hardware.camera"/>
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.HARDWARE_TEST" />
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询