在我的应用程序中,我需要处理物理按钮事件,例如音量按钮点击。为此,我正在使用 audiomanager 的 registerMediaButtonEventReceiver 方法。有一个 article与我的情况相关,但我无法使其正常工作。
这是我使用的代码:
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>
最佳答案
您的RemoteControlReceiver 类是VolumeBroadcastActivity 的内部类。你的 list 没有这么说,我认为它不能。将其设为常规类并在其 android:name 前面加上一个点。
关于android - registerMediaButtonEventReceiver/处理音量按钮问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11074345/