android锁屏显示

首先简单说一下实现的思路,在MainActivity中启动一个服务,服务中注册锁屏广播监听,监听到锁屏状态改变启动LockScreenActivity作为锁屏页面,实现如下:
首先在MainActivity的布局中写一个SwitchCompat用于开关锁屏。 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <RelativeLayout
        android:id="@+id/ll_lockscreen_setting_switch_layout"
        android:layout_width="match_parent"
        android:layout_height="74dp"
        android:background="#ffffff"
        android:orientation="horizontal"
        android:paddingLeft="10dp"
        android:paddingRight="10dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="锁屏阅读"
                android:textColor="#333333"
                android:textSize="17sp" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:text="开启后,锁屏切换成自己的"
                android:textColor="#666666"
                android:textSize="13sp" />
        </LinearLayout>

        <android.support.v7.widget.SwitchCompat
            android:id="@+id/swc_lockscreen_open"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"/>
    </RelativeLayout>

</RelativeLayout>

MainActivity的逻辑如下所示,这里没有做状态保持,实际开发可以简单的通过SP实现。 

public class MainActivity extends AppCompatActivity {

    private SwitchCompat mOpenLockScreenSWC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mOpenLockScreenSWC = (SwitchCompat) findViewById(R.id.swc_lockscreen_open);
        mOpenLockScreenSWC.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //开启锁屏服务
                    startLockScreen();
                } else {
                    //关闭锁屏服务
                    stopLockScreen();
                }
            }
        });
    }

    /**
     * 开启锁屏服务
     */
    public void startLockScreen(){
        Intent intent = new Intent(MainActivity.this,LockScreenService.class);
        this.startService(intent);
    }

    /**
     * 关闭锁屏服务
     */
    public void stopLockScreen(){
        Intent intent = new Intent(MainActivity.this,LockScreenService.class);
        this.stopService(intent);
    }

}

 下面是服务的代码。

public class LockScreenService extends Service {
    private final String TAG = "LockScreenService";

    private LockScreenBroadcastReceiver mReceiver = null;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG,"onCreate => ");
        mReceiver = new LockScreenBroadcastReceiver();

        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_USER_PRESENT);
        registerReceiver(mReceiver,filter);

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        Log.i(TAG,"onStartCommand => 启动了服务");
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG,"onDestroy => 销毁了服务");
        if(mReceiver != null){
            unregisterReceiver(mReceiver);
        }
    }
}

 广播接收的代码。

public class LockScreenBroadcastReceiver extends BroadcastReceiver {
    private final String TAG = "LockScreenBR";

    @Override
    public void onReceive(final Context context, Intent intent) {

        if (intent != null) {
            String action = intent.getAction();
            Log.i(TAG, "LockScreenBroadcastReceiver => receiver => [action : " + action + "]");

            if (action != null && action.equals(Intent.ACTION_SCREEN_ON)) {
                //开启
                try {
                    Intent startIntent = new Intent(context, LockScreenActivity.class);
                    startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
                    );
                    context.startActivity(startIntent);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }
}

 锁屏页面的布局简单的写一个。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    tools:context=".LockScreenActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher"/>

</RelativeLayout>

锁屏页面要做一些特殊的处理,比如设置锁屏显示,设置隐藏虚拟按键等,(如果需要显示时间日期就监听系统时间显示,当然还有手势解锁,滑动解锁)我这里只做简单展示。 

public class LockScreenActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置当前Activity的锁屏显示
        KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
        KeyguardManager.KeyguardLock keyguardLock = km.newKeyguardLock("1");
        keyguardLock.disableKeyguard();
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

        setContentView(R.layout.activity_lock_screen);

        //隐藏虚拟按键
        hideBottomButton();
    }

    /**
     * 隐藏虚拟按键
     */
    private void hideBottomButton() {
        try{
            //隐藏虚拟按键
            if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) {
                View v = getWindow().getDecorView();
                v.setSystemUiVisibility(View.GONE);
            } else if (Build.VERSION.SDK_INT >= 19) {
                View decorView = getWindow().getDecorView();
                int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                        | View.SYSTEM_UI_FLAG_FULLSCREEN;
                decorView.setSystemUiVisibility(uiOptions);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

AndroidManifest.xml如下。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.github.gjp.lockscreendemo">

    <uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">

        <meta-data
            android:name="android.max_aspect"
            android:value="2.2" />

        <service android:name=".LockScreenService" android:exported="false"/>

        <activity android:name=".LockScreenActivity"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:launchMode="singleInstance"
            android:taskAffinity="com.demo.lockscreen"
            android:theme="@style/lockScreenActivityTheme">

        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
 

 最后安装之后需要去设置页面给当前应用锁屏显示的权限,如下图所示。

以上的代码只是一个实现思路,如果要开发商用还有很多地方需要完善。

 
  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值