• android 接受系统锁屏广播,及高版本发送广播


    protected BroadcastReceiver messageReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (action.equals(MusicConstant.MEDIA_EXECUTE_CMD)) {
                    int cmd = Integer.parseInt(intent.getStringExtra("cmd"));
                    switch (cmd) {
                        case 1:
                            if (PCount % 2 == 0) {
                                screenOff();
                            } else {
                                screenOn();
                            }
                            PCount++;
                            break;
                        case MusicConstant.cmd.VKEYCODE_MUTE:
                            // mute();
                            break;
                        case MusicConstant.cmd.VKEYCODE_UP://up
    
                            //  volumeUp();
                            break;
                        case MusicConstant.cmd.VKEYCODE_DOWN://down
    
                            // volumeDown();
                            break;
                        case MusicConstant.cmd.VKEYCODE_EXIT:
                            //finish();
                            break;
                        default:
                            break;
    
                    }
    
                }else if(action.equals(MusicConstant.SET_VOLUME_ACTION)){
                    app.baseOnKeyFlag = false;
                  baseClickTime = System.currentTimeMillis();
                    int oldVol = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
                    setVolume(intent.getIntExtra("volume",oldVol));
                    showBaseVolumeWindow();
                }else if(action.equals(Intent.ACTION_SCREEN_ON)){
                    stopTimer();
                }else if(action.equals(Intent.ACTION_SCREEN_OFF)){
                    startTimer();
                }
            }
        };
    
        private void initFilter() {
            IntentFilter filter = new IntentFilter();
            filter.addAction(MusicConstant.MEDIA_EXECUTE_CMD);
            filter.addAction(MusicConstant.SET_VOLUME_ACTION);
            filter.addAction(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            registerReceiver(messageReceiver, filter);
        }

    1.高版本,用户发送自定义广播,接收不到信息

    Intent intent = new Intent();
    intent.setAction("demo.destroy");
    intent.setComponent( new ComponentName( "luoy.com.servicebackstage" ,
            "luoy.com.servicebackstage.MyReceiver") );
    sendBroadcast(intent);

    setComponent的两个参数,第一个是包名,第二个是Receive的路径

    2.系统广播接收不到

    一些特殊的广播,必须动态注册,静态注册是不起效果的

    类似,屏幕的锁屏和开屏

    IntentFilter recevierFilter=new IntentFilter();
    recevierFilter.addAction(Intent.ACTION_SCREEN_ON);
    recevierFilter.addAction(Intent.ACTION_SCREEN_OFF);
    MyReceiver receiver=new MyReceiver();
    registerReceiver(receiver, recevierFilter);

    https://blog.csdn.net/qq_30711091/article/details/86657268
  • 相关阅读:
    java 技术社区!!
    有关 gridview的 (做东西 容易忘记 记下!)
    【C#复习之虚方法vs抽象类】08.4.3 上午
    高分求解答!!!
    【学习笔记】asp.net MVC!
    asp.net ajax 尝试 之很好很强大!08.4.4
    Linux流量监控工具 – iftop (最全面的iftop教程)
    linux下查看某目录占用的空间大小
    基于CentOS的Linux基本网络配置,包括网卡eth0、DNS、Host等
    ubuntu安装ssh服务
  • 原文地址:https://www.cnblogs.com/pengmn/p/10416552.html
Copyright © 2020-2023  润新知