Android 一键开启手电筒

 有些Android 手机没有手机电筒的快捷方式,网上的那些软件很多都是插有广告,附带下载,还要点击进入软件....作为Android程序员的自己就自己写了一个手电筒的开关app,只需要点击桌面上的图标就可以开启手电筒,方便使用:

  原理:点击图片后, 启动service,在service里进行手电的开启与关闭,并mainActivity里判断有没有一个service在运行,如果有说明手电已经开了,再次点击图标则关闭手电,       停止service。


mainActivity  代码如下:

    @Override
    protected void onResume() {                                                                                                      //注意,这段代码写在onresume里。
        // TODO Auto-generated method stub
        super.onResume();
        if (MainActivity.isServiceWork(MainActivity.this, "com.example.light.LightService")) {     //判断有没有这个名字的service在运行
            Intent intent1 = new Intent(this, LightService.class);
            stopService(intent1);
        } else {
            Intent intent = new Intent(this, LightService.class);
            startService(intent);
        }

        finish();                                                                                                                                            //开启或关闭电筒后,关闭改页面
    }


service代码

package com.example.light;

import android.app.Service;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.IBinder;

public class LightService extends Service{
	private Camera camera; 
	private Parameters params;
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		camera = Camera.open();
	    params = camera.getParameters();
	   
	    
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		 params.setFlashMode(Parameters.FLASH_MODE_TORCH);
		 camera.setParameters(params);
	     camera.startPreview(); // 开始亮灯
		
		return START_STICKY;
	}
	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		camera.stopPreview();
		camera.release();
		camera = null;
		params = null; // 开始亮灯
	}
	
	
}


在清单文件里注册service:

<service
            android:name="com.example.light.LightService"
            android:enabled="true"
            android:exported="true"
            android:process="system"
            >
            <intent-filter
                android:priority="1000"
                ></intent-filter>
        </service>

此时运行后,可能会闪屏,可以继续如下代码:

在res里的styles里加如下代码:

   <style name="Theme.AppStartLoadTranslucent" parent="android:Theme">    
                <item name="android:windowIsTranslucent">true</item>   
            <item name="android:windowNoTitle">true</item>    
    </style> 


并在清单文件里修改application         android:theme="@style/Theme.AppStartLoadTranslucent" 。


到这边就可以了,可以自己搞张图标,就可以用啦!



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值