PHP
·
发表于 5年以前
·
阅读量:8281
public class BroadCastTest extends AppCompatActivity {
private IntentFilter intentFilter;
private NetWorkChangeReceiver networkChangeReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_broad_cast_test);
intentFilter=new IntentFilter();
intentFilter.addAction("android.media.VOLUME_CHANGED_ACTION");
networkChangeReceiver=new NetWorkChangeReceiver();
registerReceiver(networkChangeReceiver,intentFilter);
}
//动态注册,最后一定要销毁
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(networkChangeReceiver);
}
class NetWorkChangeReceiver extends BroadcastReceiver {
@Override
//接收到广播后,要执行的代码
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "音量改变 ", Toast.LENGTH_LONG).show();
}
}
}