使用getReceiverInfo时获取nullpointer异常

时间:2012-04-23 04:17:35

标签: android broadcastreceiver

我无法获取接收器文件中元数据中指定的值,因此获得空指针异常。

在我已指定的清单文件中

<receiver android:name=".SmsReceiver" >
            <intent-filter android:priority="10">

                <action
                    android:name="android.provider.Telephony.SMS_RECEIVED"
                    android:enabled="true" />
                <meta-data android:name="Algorithm" android:value="AES"/>
            </intent-filter>
        </receiver>

并在收件人iam指定

public class SmsReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        // ---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
         ActivityInfo appi = null;
        try {
            appi = context.getPackageManager().getReceiverInfo(new ComponentName(context,"intgafaes.mobileafaes.androidafaes.securesms.ADDFriendSmsReceiver"), PackageManager.GET_META_DATA);
        } catch (NameNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }   

        Bundle bundle1 = appi.metaData;

        String value = bundle1.getString("Algorithm");
        System.out.println("Value is "+value);

        if(value.equals("AES"))
        {
            System.out.println("entered into onRecieve");
        }

3 个答案:

答案 0 :(得分:0)

当您要检索值时,应始终进行空检查。

if(appi != null){
Bundle bundle1 = appi.metaData;
}

我认为它无法获得“intgafaes.mobileafaes.androidafaes.securesms.ADDFriendSmsReceiver”

..  并且你的try catch在这段代码中没用。

它没有做任何事情......至少它应该有return语句,以便不会发生异常..

public class SmsReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        // ---get the SMS message passed in---
        Bundle bundle = intent.getExtras();
         ActivityInfo appi = null;
        try {
            appi = context.getPackageManager().getReceiverInfo(new ComponentName(context,"intgafaes.mobileafaes.androidafaes.securesms.ADDFriendSmsReceiver"), PackageManager.GET_META_DATA);
        } catch (NameNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
            return;
        }   

        Bundle bundle1 = appi.metaData;

        String value = bundle1.getString("Algorithm");
        System.out.println("Value is "+value);

        if(value.equals("AES"))
        {
            System.out.println("entered into onRecieve");
        }

答案 1 :(得分:0)

在接收器中我必须在intent-filter标签之后声明元数据标签然后我可以使用相同的上述代码访问数据

<receiver android:name=".SmsReceiver" >
            <intent-filter android:priority="10">

                <action
                    android:name="android.provider.Telephony.SMS_RECEIVED"
                    android:enabled="true" />

            </intent-filter>
             <meta-data android:name="Algorithm" android:value="AES"/>
        </receiver>

答案 2 :(得分:0)

您已在AndroidManifest中指定了此元素的元数据:

<receiver android:name=".SmsReceiver" >

如果你想获得这个元素的ActivityInfo,你应该调用:

appi = context.getPackageManager().getReceiverInfo(
        new ComponentName(context, "<yourrootpackage>.SmsReceiver"),
        PackageManager.GET_META_DATA);

如果您在SmsReceiver内调用此内容(就像您提供的代码中所做的那样),您可能希望避免使用硬编码字符串,而是执行以下操作:

appi = context.getPackageManager().getReceiverInfo(
        new ComponentName(context, this.getClass()),
        PackageManager.GET_META_DATA);

或者只是复制粘贴混淆,您使用完全不同的类名查询信息?