在我的应用程序需要处理物理按键的事件,如音量按钮点击。对于我使用audiomanager的registerMediaButtonEventReceiver方法。这里有一个article有关我的情况,虽然我不能得到它的工作。
下面是code我使用的是:
公共类VolumeBroadcastActivity延伸活动{ / **当第一次创建活动调用。 * / @覆盖 公共无效的onCreate(捆绑savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.main); AudioManager经理=(AudioManager)getSystemService(AUDIO_SERVICE); manager.registerMediaButtonEventReceiver(新单元名(getPackageName(),RemoteControlReceiver.class.getName())); } 公共类RemoteControlReceiver扩展广播接收器{ @覆盖 公共无效的onReceive(上下文的背景下,意图意图){ Toast.makeText(VolumeBroadcastActivity.this,1,1).show(); 如果(Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())){ } } }}< XML版本=1.0编码=UTF-8&GT?;<清单的xmlns:机器人=http://schemas.android.com/apk/res/android 包=localhost.volume.broadcast 安卓版code =1 机器人:=的versionName1.0> <采用-SDK安卓的minSdkVersion =10/> <应用 机器人:图标=@绘制/ ic_launcher 机器人:标签=@字符串/ APP_NAME> <活动 机器人:名字=。VolumeBroadcastActivity 机器人:标签=@字符串/ APP_NAME> &所述;意图滤光器> <作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/> &所述; /意图滤光器> < /活性GT; <接收机器人:名字=RemoteControlReceiver> &所述;意图滤光器> <作用机器人:名字=android.intent.action.MEDIA_BUTTON/> &所述; /意图滤光器> < /接收器> < /用途>< /清单>
解决方案
您 RemoteControlReceiver
类是一个内部类 VolumeBroadcastActivity 。您的清单中没有这么说,我不认为它可能。使它成为一个普通班和precede其
的android:名称
以点
In my application I need to process physical button events such as volume button clicks. For that I'm using audiomanager's registerMediaButtonEventReceiver method. There's an article relevant to my situation, though I can't get it working.
Here's code I'm using:
public class VolumeBroadcastActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
manager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), RemoteControlReceiver.class.getName()));
}
public class RemoteControlReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(VolumeBroadcastActivity.this, "1",1).show();
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="localhost.volume.broadcast"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".VolumeBroadcastActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
</application>
</manifest>
解决方案
Your RemoteControlReceiver
class is an inner class of VolumeBroadcastActivity
. Your manifest doesn't say so and I don't think it could. Make it a regular class and precede its android:name
with a dot.