Android 锁屏后不被暂停的奇淫巧计

据说是某狗音乐的方案。现在的情况是锁屏后线程、主进程都停止运行,建立服务置为前台服务,绑定通知都不行。

但是一些音乐类软件还能后台播放,所以打开脑洞,一直播放一个无声的音频能不能保持锁屏后进程运行。

测试后确实可行。前台服务/绑定通知+循环播放无声音频。

 

public class MService extends Service {

    Notification notification;
    private Context mContext;
    public static Thread thread;
    private MediaPlayer mediaPlayer;
    private boolean isrun = true;

    public MService() {
    }

    public static final String CHANNEL_ONE_ID = "nyd001";
    public static final String CHANNEL_ONE_NAME = "诺秒贷";

    private static class MyHandler extends Handler {
        private WeakReference<MService> reference;
        public MyHandler(MService service) {
            reference = new WeakReference<>(service);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                //todo
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            sendEmptyMessageDelayed(0, 5000);
        }
    }

    MyHandler handler;


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

        mContext = this;

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
                notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {

            Notification.Builder builder = new Notification.Builder(mContext)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setWhen(System.currentTimeMillis())
                    .setTicker("music")
                    .setContentTitle("标题")
                    .setContentText("内容")
                    .setOngoing(true)
                    .setPriority(PRIORITY_MAX)
                    .setContentIntent(pendingIntent)
                    .setAutoCancel(false);

            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                 //修改安卓8.1以上系统报错
                 NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ONE_ID, CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_MIN);
                 notificationChannel.enableLights(false);//如果使用中的设备支持通知灯,则说明此通知通道是否应显示灯
                 notificationChannel.setShowBadge(false);//是否显示角标
                 notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);
                 NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
                 manager.createNotificationChannel(notificationChannel);
                 builder.setChannelId(CHANNEL_ONE_ID);
            }

            notification = builder.build();
        }
        /*使用startForeground,如果id为0,那么notification将不会显示*/
        startForeground(100, notification);

        
        if(thread == null){
            thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    
                    while (isrun){

                        
                        try {
                            Thread.sleep(5000);
                            try {
                                //todo
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                           

                            return;
                        }

                    }
                }
            });
        }

        handler = new MyHandler(this);
//        handler.sendEmptyMessage(0);
        thread.start();

        
        if(mediaPlayer == null){
            mediaPlayer = MediaPlayer.create(this,R.raw.test);
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
        }

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onDestroy() {
        isrun = false;
        stopForeground(true);
        thread.interrupt();
        mediaPlayer.release();
        handler.removeMessages(0);
        stopSelf();
        super.onDestroy();
    }
}

 

Intent forgroundService = new Intent(this,MService.class);
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                startForegroundService(forgroundService);
            } else {
                startService(forgroundService);
            }


Intent forgroundService = new Intent(this,MService.class);
            stopService(forgroundService);

 

点赞