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