我想知道以编程方式注册广播接收器的最佳实践/方法是什么.我想根据用户的选择注册特定的接收器.
由于注册是通过清单文件完成的,我想知道是否有一种正确的方法可以在代码中实现这一点.
我想在我的主要活动中创建一个广播接收器作为内部类.但我在清单xml文件中定义广播接收器时遇到问题,因为android无法找到它.
码:
public class MyActivity extends Activity{
...
public class Receiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
....
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
表现:
<receiver android:name=".org.danizmax.myapp.MyActivity$Receiver" android:enabled="true">
<intent-filter>
<action android:name="org.danizmax.myapp.BROADCAST_INITIAL_DATA"></action>
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
我尝试过:
我看到其他人也有类似的问题,但没有找到任何答案.
那有可能吗?如果没有,有什么更好的方式来使用广播接收器?
谢谢!
目标是拦截来自耳机的广播以及最终的蓝牙,以响应来自耳机的不同类型的点击以改变媒体播放器.此解决方案适用于ICS之前的所有版本.这是我尝试过的一些代码和事情:
....
private BroadcastReceiver mediaButtonReceiver = new MediaButtonIntentReceiver();
....
public void onCreate() {
...
IntentFilter mediaFilter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);
mediaFilter.setPriority(2147483647); // this is bad...I know
this.registerReceiver(mediaButtonReceiver, mediaFilter);
...
}
public class MediaButtonIntentReceiver extends BroadcastReceiver {
private KeyEvent event;
public MediaButtonIntentReceiver() {
super();
}
@Override
public void onReceive(Context context, Intent intent) {
String intentAction = intent.getAction();
if (!Intent.ACTION_MEDIA_BUTTON.equals(intentAction)) {
return;
}
event = (KeyEvent)intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
if (event == null) {
return;
}
try {
int action = event.getAction();
switch(action) {
case KeyEvent.ACTION_UP …Run Code Online (Sandbox Code Playgroud) android media-player android-intent android-4.0-ice-cream-sandwich
我按下时设法让我的应用程序识别我的耳机按钮,但其中一个按钮需要调用MyCustomActivity中的方法.问题是onReceive的第一个参数是一个不能强制转换为Activity的Context,并且使用MyCustomActivity的内部类 在Android 4.1中不起作用,除非它是静态的(具有无法访问MyCustomActivity方法的相同问题).
所以我唯一的选择(为了支持2.x和4.1)是将活动作为参数传递给RemoteControlReceiver.
但是,如何实现它的唯一方法是通过:
private ComponentName mRemoteControlReceiver = new ComponentName(this, RemoteControlReceiver.class);
Run Code Online (Sandbox Code Playgroud)
哪个不接受任何其他参数?
任何想法如何解决这个限制?
注意:如果我尝试定义RemoteControlReceiver具有参数的构造函数,则会收到以下异常:
E/AndroidRuntime(2836): java.lang.RuntimeException: Unable to instantiate receiver com.example.RemoteControlReceiver: java.lang.InstantiationException: can't instantiate class com.example.RemoteControlReceiver; no empty constructor
Caused by:
E/AndroidRuntime(2836): Caused by: java.lang.InstantiationException: can't instantiate class com.example.RemoteControlReceiver; no empty constructor
E/AndroidRuntime(2836): at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(2836): at java.lang.Class.newInstance(Class.java:1319)
E/AndroidRuntime(2836): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2205)
Run Code Online (Sandbox Code Playgroud)
很明显,这个新的registerMediaButtonEventReceiver要求(在Android 4.1中引入)需要一个空的构造函数.
有没有办法解决这个问题?
例如,有没有办法获得对实际RemoteControlReceiver对象的引用(通过间接实例化mAudioManager.registerMediaButtonEventReceiver())?这样我可以使用访问器在实例化后设置RemoteControlReceiver的数据成员吗?
在我的应用程序中,我需要处理物理按钮事件,例如音量按钮点击.为此,我使用了audiomanager的registerMediaButtonEventReceiver方法.有一篇文章与我的情况有关,虽然我无法让它发挥作用.
这是我正在使用的代码:
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> …Run Code Online (Sandbox Code Playgroud)