android------锁屏(手机启动出现锁屏界面)

以前用过一个红包锁屏的软件,第一次打开手机出现锁屏,滑动领取收益,当时觉得这功能不错,就查阅资料,写了一个案例,

apk运行流程: 进入软件---》启动服务---》关闭手机(可先退出应用)--》再打开手机即可看见锁屏界面

效果图:

                

当然这个案例还是有缺点的,没考虑性能问题。

界面是可以随意修改的,滑动的是一个自定义控件。

服务类

public class AppService extends Service {

    private AppReceiver mLockScreenReceiver;
    
    private IntentFilter mIntentFilter = new IntentFilter();
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 监听屏幕关闭和打开的广播必须动态注册
        mIntentFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
        mIntentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        mIntentFilter.addAction(Intent.ACTION_SCREEN_ON);
        mIntentFilter.addAction(Intent.ACTION_TIME_TICK);
        // 设置广播的优先级
        mIntentFilter.setPriority(Integer.MAX_VALUE);
        if (null == mLockScreenReceiver) {
            mLockScreenReceiver = new AppReceiver();
            mIntentFilter.setPriority(Integer.MAX_VALUE);
            registerReceiver(mLockScreenReceiver, mIntentFilter);
            Toast.makeText(getApplicationContext(), "AppService", Toast.LENGTH_LONG).show();
            
            
        }
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setTicker("APP正在运行");
        builder.setAutoCancel(false);
        builder.setContentTitle("APP正在运行");
        builder.setContentText("您的收益正在累积");
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setContentIntent(PendingIntent.getActivity(this, 0, new Intent(this, LockScreenActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));
        Notification n = builder.build();
        // 通知栏显示系统图标
        startForeground(0x111, n);
        
        Parser.killBackgroundProcess(this);
        return START_STICKY;
    }
    
    @Override
    public void onDestroy() {
        if (mLockScreenReceiver != null) {
            unregisterReceiver(mLockScreenReceiver);
            mLockScreenReceiver = null;
        }
        super.onDestroy();
        // 重启服务
        startService(new Intent(this, AppService.class));
        
        
    }

}

源码有点多就不一一贴出来了,直接下载源码即可。

有兴趣的小伙伴可以参考,一起研究。

源码点击下载:https://github.com/DickyQie/android-system

原文地址:https://www.cnblogs.com/zhangqie/p/8431566.html