• 一个最简单的自定义锁屏应用实现


    实现思路

    1. 该锁屏的实现思路是在锁屏显示的时候,屏蔽掉系统原来的屏保,显示自己的屏保程序
    2. 锁屏调用的时机有两个,一个是在手机开机时(可以监听开机广播),第二个是在屏幕灭屏/亮屏切换时候(可以监听屏幕灭/亮的广播)。

    实现

    SystemEventReceiver

    接受系统开机广播的Receiver,并启动Service

    package com.lockscreen;
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    /**
     * Created by jun on 17-4-24.
     */
    public class SystemEventReceiver extends BroadcastReceiver {
    
        @Override
    
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
                context.startService(new Intent(context, LockScreenService.class));
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    MainActivity

    锁屏显示的界面

    package com.lockscreen;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    
    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            findViewById(R.id.activity_main).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    MainActivity.this.startService(new Intent(MainActivity.this, LockScreenService.class));
                }
            });
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    AndroidManifest.xml

    
    
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.lockscreen">
    
        
        <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
    
        
        <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED">uses-permission>
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
    
            <activity
                android:name=".MainActivity"
                android:excludeFromRecents="true">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                intent-filter>
            activity>
    
            <service android:name=".LockScreenService">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                intent-filter>
            service>
    
            
            <receiver android:name=".SystemEventReceiver">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                intent-filter>
            receiver>
        application>
    manifest>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    LockScreenService

    处理锁屏的Service

    处理锁屏的Service
            package com.lockscreen;
    
    import android.app.KeyguardManager;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.app.Service;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.graphics.Color;
    import android.os.IBinder;
    import android.support.annotation.Nullable;
    import android.util.Log;
    
    /**
     * Created by jun on 17-4-24.
     */
    
    public class LockScreenService extends Service {
        private final String TAG = this.getClass().getName();
    
    
        private KeyguardManager km;
        private KeyguardManager.KeyguardLock kk;
    
        private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d(TAG, "onReceive: start");
    
                KeyguardManager km = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
                KeyguardManager.KeyguardLock kk = km.newKeyguardLock("");
                kk.disableKeyguard();
    
                Intent service = new Intent();
                service.setClass(context, MainActivity.class);
                service.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(service);
            }
        };
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
            kk = km.newKeyguardLock("");
            kk.disableKeyguard();
        }
    
        @Override
        public void onStart(Intent intent, int startId) {
            super.onStart(intent, startId);
            Log.d(TAG, "onStart: ");
            //通知状态条
            notifySpinnerBar();
    
            //亮屏/灭屏广播只能动态监听
            IntentFilter iFilter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    
            iFilter.setPriority(1000);
    
            this.registerReceiver(broadcastReceiver, iFilter);
        }
    
        //将本服务显示到SpinnerBar上
    
        private void notifySpinnerBar() {
            Notification notify = new Notification(R.mipmap.ic_launcher, null, 0);
    
            //将此通知放到通知栏的Ongoing组中,也就是正在运行的组中
            notify.flags |= Notification.FLAG_ONGOING_EVENT;
    //        notify.flags |= Notification.FLAG_AUTO_CANCEL;
    //        notify.flags |= Notification.FLAG_FOREGROUND_SERVICE;
    //        notify.flags |= Notification.FLAG_INSISTENT;
    //        notify.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    //        notify.flags |= Notification.FLAG_SHOW_LIGHTS;
    //        notify.flags |= Notification.FLAG_NO_CLEAR;
    
    
            //定义Notification出现声音
            notify.defaults |= Notification.DEFAULT_SOUND;
    
            //设置如何震动
            notify.defaults |= Notification.DEFAULT_LIGHTS;
    
            //设置lec灯颜色
            notify.ledARGB = Color.BLUE;
            notify.ledOnMS = 5000;
    
            Intent notifyIntent = new Intent(this, MainActivity.class);
            PendingIntent pIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
    //        notify.setLatestEventInfo(this,null,null,contentIntent);
    
    
            NotificationManager notifyManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
            this.startForeground(0, notify);
            //当id=0时,notify将不会显示
    //        this.startForeground(0, notify);
        }
    
    
        @Override
        public void onDestroy() {
            kk.reenableKeyguard();
            super.onDestroy();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
  • 相关阅读:
    【LeetCode】【剑指offer】【二叉搜索树的第k大节点】
    深入浅出【图卷积神经网络GCN】从 邻接矩阵、特征值矩阵、单位阵、度矩阵 入手,深刻理解融合邻居节点(信息) | GCN从公式到代码实现 全过程 | 在Cora数据集上实现节点分类任务
    数字化时代,传统IT和数字型IT能否严格区分?
    Design for failure常见的12种设计思想
    3D调研-摄像头
    《数据结构与算法》-栈的概念和栈的实现
    Vue2:网易云播放音乐并实现同步一次显示一行歌词
    第二章 进程与线程 十二、进程同步与进程互斥
    操作系统 内存对齐
    第5讲:SQL语句之DML类型的数据操纵语言
  • 原文地址:https://blog.csdn.net/Jun_P/article/details/126734985