振铃模式更改监听器广播接收器?
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;
}
从上面的代码我可以得到铃声模式。我想做的是监听铃声模式的变化并调用一个函数。
我被告知我可以注册 AudioManager。 RINGER_MODE_CHANGED_ACTION 并在broadcastreceiver onReceive 方法中监听更改意图。听起来很清楚。但我是android新手,真的不知道怎么写。有没有人可以写一段代码并展示它到底是如何工作的,而不是说使用这个或那个:)谢谢
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;
}
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在要处理广播的
Activity
或Service
的onCreate()
方法中使用以下代码:Use the following code inside the
onCreate()
method of yourActivity
orService
that you want to process the broadcast:另一个解决方案是在 Manifest 中添加一个带有操作的接收器:
并且您的类 RingerModeStateChangeReceiver 应该扩展 BroadcastReceiver。
Another solution is to add a receiver with an action in Manifest:
and your class RingerModeStateChangeReceiver should extend BroadcastReceiver.
这是 Kotlin 的更新版本。将其放置在您的
onCreate()
生命周期下。Here's an update version in Kotlin. Place this under your
onCreate()
lifecycle.