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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM