林格模式改变监听广播接收器? [英] Ringer mode change listener Broadcast receiver?
问题描述
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
Log.i("MyApp","Silent mode");
break;
case AudioManager.RINGER_MODE_VIBRATE:
Log.i("MyApp","Vibrate mode");
break;
case AudioManager.RINGER_MODE_NORMAL:
Log.i("MyApp","Normal mode");
break;
}
从code以上我可以得到振铃模式。我会liek做的就是听振铃模式的变化和调用一个函数。
From the code above I can get the ringer mode. What I would liek to do is listen the ringer mode changes and call a function.
我被告知的是,我可以注册AudioManager。 RINGER_MODE_CHANGED_ACTION和收听的BroadcastReceiver的onReceive方法的改变意图。这听起来很清楚。但是,我是新来的Android和真的不知道怎么写。是否有任何人可以只写一张code和表现究竟如何它的工作原理,而不是说用这样或那样的:)谢谢
What I have been told is that I can register the AudioManager. RINGER_MODE_CHANGED_ACTION and listen the change intent in broadcastreceiver onReceive method. It sounds clear. But I am new to android and really dont know how to write it. Is there any one can just write a piece of code and show how exactly it works instead of saying use this or that :) Thank you
推荐答案
使用下面的code你的的onCreate()
方法内>活动或服务
,你要处理的广播:
Use the following code inside the onCreate()
method of your Activity
or Service
that you want to process the broadcast:
BroadcastReceiver receiver=new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
//code...
}
};
IntentFilter filter=new IntentFilter(
AudioManager.RINGER_MODE_CHANGED_ACTION);
registerReceiver(receiver,filter);
这篇关于林格模式改变监听广播接收器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!