在我的代码中有一个扩展的内部类BroadcastReceiver.
我已将以下行添加到AndroidManifest.xml:
<receiver android:name="OuterClass$InnerClass android:enabled="true"/>
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
无法实例化接收器org.example.test.OuterClass $ InnerClass
我该如何解决这个问题?
目标是拦截来自耳机的广播以及最终的蓝牙,以响应来自耳机的不同类型的点击以改变媒体播放器.此解决方案适用于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
我有非常基本的问题.它可能很简单,但我没有得到它.我有一个Activity,我正在使用一些UI组件.我还有一个广播接收器(从清单注册) ,我需要更新Activity类的一些UI组件.喜欢 -
Class MyActivity extends Activity
{
onCreate(){
//using some UI component lets say textview
textView.setText("Some Text");
}
updateLayout()
{
textView.setText("TextView Upadated...");
}
}
Class broadCastReceiver
{
onReceive()
{
//here I want to update My Activity component like
UpdateLayout();
}
}
Run Code Online (Sandbox Code Playgroud)
为此 - 一个解决方案是使updateLayout()方法公共静态,并通过活动引用在接收器类中使用该方法.但我认为,这不是正确的方法.有没有正确的方法来做到这一点?
我有一个活动,我有一个广播接收器(br).如果我以编程方式注册br,则接收器已注册并且工作正常.
但是,如果我在清单中注册接收器,我会收到java.lang.ClassNotFoundException.
<receiver
android:name=".MyActivity.UpdateUIClass"
android:exported="false"
>
<intent-filter>
<action android:name="com.mydomain.main.FILTER_UPDATE_UI" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
请指教
我设法让我的耳机按钮在按下时被我的应用程序识别,但其中一个按钮需要调用 MyCustomActivity 中的方法。问题是 onReceive 的第一个参数是一个无法转换为 Activity 的上下文,因此我被迫将 BroadcastReceiver 实现为 MyCustomActivity 中的内部类。
到目前为止一切顺利,但如何在清单中注册这个内部 MediaButtonEventReceiver?
对于独立类,这很简单:
<receiver android:name=".RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
对 MyCustomActivity 的 mReceiver 执行相同操作的技巧/语法是什么?
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context ctx, Intent intent) {
// ...
}
}
Run Code Online (Sandbox Code Playgroud)