接收器在Android的内部类 [英] Receiver as inner class in Android

查看:139
本文介绍了接收器在Android的内部类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有延伸的BroadcastReceiver一个内部类。

I am having an inner class which extends BroadcastReceiver.

和我添加以下行的Andr​​oidManifest.xml:

And I have added following line to AndroidManifest.xml:

<receiver android:name="OuterClass$InnerClass android:enabled="true"/>

不过,我收到错误无法实例接收org.example.test.OuterClass $将InnerClass:

But I am getting error Unable to instantiate receiver org.example.test.OuterClass$InnerClass:

请告诉我这个问题?

推荐答案

这是(非静态) 内部类不能被通过AndroidManifest.xml中的Andr​​oid(实例在BroadcastReceiver的 Android开发者文档):

An (non-static) inner class cannot be instantiated by Android via the AndroidManifest.xml (Android developer documentation on BroadcastReceiver):

您可以动态地注册这个类的一个实例   Context.registerReceiver()或静态发布实施   通过在AndroidManifest.xml中的标签。

You can either dynamically register an instance of this class with Context.registerReceiver() or statically publish an implementation through the tag in your AndroidManifest.xml.

所以,你可以动态注册的接收器。在我的应用程序,我想使用谷歌的云到设备消息(C2DM),做同样的我原来的Andr​​oidManifest.xml包含:

So you can dynamically register the receiver. In my application I wanted to do the same for using Google's Cloud to Device Messaging (C2DM) and my original AndroidManifest.xml contained:

<application...>
    <receiver android:name=".MyC2dmReceiver" android:permission="com.google.android.c2dm.permission.SEND">
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="com.example.myapp" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="com.example.myapp" />
        </intent-filter>
    </receiver>
</application>

我删除的接收器的部分和动态注册的接收器,如下所示:

I removed that receiver section and dynamically registered the receiver as follows:

public class AndroidService extends IntentService
{
    ... 
    @Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
        IntentFilter filter = new IntentFilter();
        filter.addAction("com.google.android.c2dm.intent.RECEIVE");
        filter.addAction("com.google.android.c2dm.intent.REGISTRATION");
        filter.addCategory("com.example.myapp");
        this.registerReceiver(new MyC2dmReceiver(), filter, "com.google.android.c2dm.permission.SEND", null);
        return super.onStartCommand(intent,flags,startId);
    }

    public class MyC2dmReceiver extends BroadcastReceiver
    {
        ...
    }
}

这篇关于接收器在Android的内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆