简体   繁体   English

没有为SMS_RECEIVED调用静态广播接收器

[英]Static Broadcast Receiver not being called for SMS_RECEIVED

i am writing a broadcastreciever to listen for SMS_RECEIVED action .it works perfectly fine if i register it dynamically but if i register it statically in the Manifest file the broadcast receiver is not being called. 我正在编写一个广播接收器来侦听SMS_RECEIVED动作。如果我动态注册它,则工作得很好,但是如果我在清单文件中静态注册它,则不会调用广播接收器。

<receiver android:name=".MyReceiverBroadcast"><intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECIEVED"></action>
        </intent-filter></receiver>

My broadcast reciever is as follows 我的广播接收者如下

public void onReceive(Context context, Intent intent) {
    String action=intent.getAction();
        if(action.equals(SMS_RECEIVED_ACTION))
        {
Log.v("sms", "reciever called");
            Object[] messages=(Object[])intent.getExtras().get("pdus");
            for(Object message:messages)
            {
                byte[] messagedata=(byte[])message;
                SmsMessage smsmessage=SmsMessage.createFromPdu(messagedata);
                processmessage(smsmessage,context);
            }

        }


    }

    private void processmessage(SmsMessage smsmessage,Context c) {
        String from=smsmessage.getOriginatingAddress();
        String messcontent=smsmessage.getMessageBody();
        Toast.makeText(c,"from ="+from+" and mess="+messcontent,Toast.LENGTH_LONG).show();
        Log.v("sms","from ="+from+" and mess="+messcontent);
}

My Manifest file 我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tech.myfirst" >

    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_SMS"></uses-permission>

    <application
        android:name=".MyApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version"/>
        <activity
            android:name=".SmsReceiverDemo"
            android:label="SmsReciever Demo"

            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name="com.example.tech.myfirst.MyReceiverBroadcast"><intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECIEVED"></action>
        </intent-filter></receiver>

    </application>


</manifest>

I have added all the necessary permissions. 我已经添加了所有必要的权限。 What i am missing here. 我在这里想念的是什么。 i am running the program on emulator nexus 5 api 21. thanks 我在模拟器nexus 5 api 21上运行程序。谢谢

Assuming everything else is correct, it appears the problem is that you've misspelled RECEIVED in the <intent-filter> 's <action> for the <receiver> entry in the manifest. 假设其他所有内容都正确,那么问题似乎出在清单的<receiver>条目的<intent-filter><action> ,您将RECEIVED拼错了。 It should be: 它应该是:

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

You might also want to check the value of the SMS_RECEIVED_ACTION String. 您可能还需要检查SMS_RECEIVED_ACTION字符串的值。

I've had this problem before, and was solved by changing the attribute 我以前遇到过这个问题,并且通过更改属性解决了

android:name=".MyReceiverBroadcast"

To

android:name="my.package.name.MyReceiverBroadcast"

I don't know why this is happening, but it worked for me. 我不知道为什么会这样,但是对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM