当广播接收器使用InstantiationException的newInstance [英] InstantiationException when using newInstance on BroadcastReceiver

查看:219
本文介绍了当广播接收器使用InstantiationException的newInstance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,有很多方便的静态方法的类。其中一人应动态地启动一个BroadcastReceiver - 但它总是返回InstantiationException。该广播接收器有一个无参数的构造函数,因此它应该工作 - 但事实并非如此。这是我做了迄今为止:

I do have a class with lots of static convenience methods. One of them should start a BroadcastReceiver dynamically - but it always returns an InstantiationException. The BroadcastReceiver has a no-parameter constructor so it should work - but it doesn't. Here's what I did so far:

下面是它的类的简便方法:

Here's the convenience method in it's class:

// Static convenience methods in a tools class
public class MyTools {

    // Start BroadcastReceiver dynamically
    public static BroadcastReceiver startBroadcastReceiver(Context context,
            Class<? extends BroadcastReceiver> receiverClass, String receiverTag) {

        BroadcastReceiver receiver = null;

        try {
            receiver = (BroadcastReceiver) receiverClass.newInstance();
            if (receiver != null) {
                IntentFilter intentFilter = new IntentFilter(receiverTag);
                if (intentFilter != null) {
                    context.registerReceiver(receiver, intentFilter);
                }
            }
        } catch (Exception exception) {
            // --> InstantiationException
        }

        return receiver;
    }

    // ...
}

下面是用的BroadcastReceiver将InnerClass试图启动与此便捷方法的广播接收器的活动:

Here's an activity with an InnerClass BroadcastReceiver that tries to start the BroadcastReceiver with this convenience method:

// An activity with an InnerClass BroadcastReceiver
public class MyActivity extends Activity {

    public class MyBroadcastReceiver extends BroadcastReceiver {

        public static final String TAG = "aa.bb.cc.MyActivity.MyBroadcastReceiver";

        public static final long ACTION_UNDEFINED = 0;
        public static final long ACTION_DOSOMETHING = 1;

        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent != null) {
                Bundle bundleExtras = intent.getExtras();
                if (bundleExtras != null) {
                    long action = bundleExtras.getLong("ACTION");
                    if (action == ACTION_DOSOMETHING) {
                        doSomething();
                    }
                }
            }
        }
    }

    private MyBroadcastReceiver receiver;

    @Override
    protected void onResume() {
        super.onResume();

        // Start BroadcastReceiver
        receiver = (MyBroadcastReceiver) MyTools.startBroadcastReceiver(this,
                MyBroadcastReceiver.class, MyBroadcastReceiver.TAG);
    }


    public void doSomething() {
        // ...
    }
}

有什么不对这种做法?

What's wrong with this approach?

任何帮助是非常AP preciated。

Any help is highly appreciated.

推荐答案

这就是问题所在。

让你广播接收机静态内部类
公共静态类MyBroadcastReceiver扩展了BroadcastReceiver

make you broadcast receiver a static inner class public static class MyBroadcastReceiver extends BroadcastReceiver

或宣布在它自己的文件

从实例例外的没有空的构造一封邮件可能会造成混淆。

The no empty constructor messsage from the instantiation exception can be confusing

这篇关于当广播接收器使用InstantiationException的newInstance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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