【Android】实战开发之锁屏Activity实现步骤清晰简述

Android 二呆 2017-6-11

Android实战开发中,锁屏App占市场已不是小数,技术上其实也已经广为流传,首先需要知道锁屏实际上只是一个Activity而已,而开发一款锁屏App大致步骤都差不多,拢共就那么几步,为更清晰的了解以下以从文件分类角度进行说明:

一、首先,清单文件AndroidManifest.xml

1、添加权限:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />

2、activity:

<activity
            android:name="com.example.LockActivity"
            android:launchMode="singleTask"
            android:excludeFromRecents="true" >
            <intent-filter>
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
</activity>

3、service:
<service android:name="com.example.LockService" >

4、receiver:
<receiver android:name="com.unluck.receiver.BootReceiver">
            <intent-filter android:priority="1000" >
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <category android:name="android.intent.category.HOME" />
                <category android:name="android.intent.category.LAUNCHER" />
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.DATE_CHANGED" />
                <action android:name="RestartSerivcesForSystemEventReceiver" />
                <action android:name="android.intent.action.MEDIA_MOUNTED" />
                <action android:name="android.intent.action.MEDIA_UNMOUNTED" />
                <action android:name="android.intent.action.MEDIA_EJECT" />
                <data android:scheme="file" />
            </intent-filter>
</receiver>

二、开机广告接收器BootReceiver
public class BootReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        context.startService(new Intent(context,LockService.class));//此Service可在首次启动Activity中启动
    }
}

三、后台运行的锁屏服务LockService
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import com.tongleer.view.LockActivity;
/**
 * 锁屏Service,用于监听屏幕变亮或变暗事件
 * @author tongleer.com
 *
 */
public class LockService extends Service{
    private ScreenOnReceiver screenOnReceiver;
    private ScreenOffReceiver screenOffReceiver;
    @Override
    public void onCreate() {
        super.onCreate();
        screenOnReceiver=new ScreenOnReceiver();
        IntentFilter screenOnFilter=new IntentFilter();
        screenOnFilter.addAction("android.intent.action.SCREEN_ON");
        registerReceiver(screenOnReceiver, screenOnFilter);
        screenOffReceiver=new ScreenOffReceiver();
        IntentFilter screenOffFilter=new IntentFilter();
        screenOffFilter.addAction("android.intent.action.SCREEN_OFF");
        registerReceiver(screenOffReceiver, screenOffFilter);
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_STICKY;
    }
    /**
     * 监听屏幕变亮的广播接收器,变亮就屏蔽系统锁屏
     * @author tongleer.com
     *
     */
    private class ScreenOnReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String action=intent.getAction();
            if(action.equals("android.intent.action.SCREEN_ON")){
                /*
                 * 此方式已经过时,在activtiy中编写
                 * getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
                 * getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
                 * 两句可以代替此方式
                 */
                /*KeyguardManager keyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
                KeyguardLock lock = keyguardManager.newKeyguardLock("");
                lock.disableKeyguard();*/
            }
        }
    }
    /**
     * 监听屏幕变暗的广播接收器,变暗就启动应用锁屏界面activity
     * @author tongleer.com
     *
     */
    private class ScreenOffReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            String action=intent.getAction();
            if(action.equals("android.intent.action.SCREEN_OFF")){
                Intent i1=new Intent(context,LockActivity.class);
                i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i1);
            }
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(screenOnReceiver);
        unregisterReceiver(screenOffReceiver);
    }
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}

四、锁屏界面LockActivity

1、在onCreate中执行以下代码:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);

 2、重写onKeyDown方法并返回false使锁屏界面禁用回退键:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    return false;
}
3、如果想禁用Home键可以使用WindowManager弹出的View作为作为锁屏界面,因此,可以自定义View;否则不禁用Home键也可以,具体实现方法,已给出实现开启和关闭锁屏的工具类,至于自定义View那只能自由发挥了,不过方法肯定都是继承View或者View子类,重写onDraw和onTouchEvent等。

(1)锁屏工具类

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
/**
 * 锁屏工具类
 * @author tongleer.com
 * 
 */
public class LockUtil {
    private Context mContext;
    private WindowManager mWindowManager;// 窗口管理器
    private View mLockView;// 锁屏视图
    private WindowManager.LayoutParams wmParams;
    private static LockUtil mLockUtil;
    private boolean isLocked;// 是否锁定
    public static synchronized LockUtil getInstance(Context mContext) {
        if (mLockUtil == null) {
            mLockUtil = new LockUtil(mContext);
        }
        return mLockUtil;
    }
    /**
     * 构造方法
     * @param mContext
     */
    public LockUtil(Context mContext) {
        this.mContext = mContext;
        init();
    }
    /**
     * 初始化布局
     */
    private void init() {
        isLocked = false;
        // 获取WindowManager
        mWindowManager = (WindowManager) mContext.getApplicationContext().getSystemService("window");
        // 设置LayoutParams(全局变量)相关参数
        wmParams = new WindowManager.LayoutParams();
        wmParams.type = LayoutParams.TYPE_SYSTEM_ERROR;// 关键部分
        wmParams.format = PixelFormat.RGBA_8888; // 设置图片格式,效果为背景透明
        // 设置Window flag
        wmParams.width = WindowManager.LayoutParams.FILL_PARENT;
        wmParams.height = WindowManager.LayoutParams.FILL_PARENT;
        wmParams.flags = 1280;
    }
    /**
     * 锁屏
     */
    public synchronized void lock() {
        if (mLockView != null && !isLocked) {
            mWindowManager.addView(mLockView, wmParams);
        }
        isLocked = true;
    }
    /**
     * 解锁
     */
    public synchronized void unlock() {
        if (mWindowManager != null && isLocked) {
            mWindowManager.removeView(mLockView);
        }
        isLocked = false;
    }
    /**
     * 更新
     */
    public synchronized void update() {
        if (mLockView != null && !isLocked) {
            mWindowManager.updateViewLayout(mLockView, wmParams);
        }
    }
    /**
     * 设置锁屏视图
     * @param v
     */
    public synchronized void setLockView(View v) {
        mLockView = v;
    }
}

(2)在锁屏界面activity中创建对象并开启锁屏
private LockUtil lockLayer;
private View lockView;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    lockView = View.inflate(this, R.layout.main, null);
    lockLayer = new LockUtil(this);
    lockLayer.setLockView(lockView);// 设置要展示的页面
    lockLayer.lock();// 开启锁屏
    CustomView.setHandler(mHandler);
}
(3)从第(2)步可以发现,你的自定义View可以传一个Handler参数,方便从自定义View中传递数据到Activity中,到此为止,锁屏App大致步骤已经结束,快去做一个锁屏App吧!
  • X
    打赏
    • 微信
    • QQ红包
    • 支付宝


    用微信扫一扫

    您的鼓励将是站长及作者前行的动力,谢谢打赏
    (若二维码有误可举报)
  • 0
  • 0
  • 881

Powered by emlog Theme By Tongleer

sitemap