I am unable to get a SMS Broadcast Receiver to wor

2019-08-03 04:49发布

I am new to this. I am working on a simple SMS receiver that catches incoming text messages, does some logic with them and then forwards them to my email for more permanent storage. The broadcast receiver is registered in my manifest and the code works great when I test on the Galaxy S3. However, when I install it on a HTC One it seems that the Boradcast Receiver is never fired. Just wondering if anyone else has run into this on HTC and if there is any suggested course of action. It's frustrating that it works on my Samsung, but not on HTC.

Note: I have tried all of the commonly found answers online (increase priority, make sure all permissions are in manifest, tried it in an activity, etc.). The catlog doesn't seem to be helpful either. If I set a break point at the beginning of the onReceive() method or make toast there, neither points of code are ever hit on the HTC.

Here is my manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sms.forward"
    android:versionCode="1"
    android:versionName="1.1" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.RECEIVE_WAP_PUSH" />
    <uses-permission android:name="android.permission.PERSISTENT_ACTIVITY" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" /> 
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Light" >
        <service android:name=".SecureMessagesActivity" />  
        <receiver android:name="com.android.upsync.SmsReceiver" android:exported="true" android:permission="android.permission.BROADCAST_SMS"> 
            <intent-filter android:priority="2147483647" > 
                <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>                    
                <category android:name="android.intent.category.DEFAULT"></category>                                    
            </intent-filter>
        </receiver>
    </application>
</manifest>

AND my code that receivers it:

    @Override
        public void onReceive(Context context, Intent intent)
        {   

            Bundle extras = intent.getExtras();        
            String messageBody = "";
            if ( extras != null )
            {
                // Get received SMS array
                Object[] smsExtra = (Object[]) extras.get( SMS_EXTRA_NAME );

                for ( int i = 0; i < smsExtra.length; ++i )
                {
                    SmsMessage  sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
                    messageBody = sms.getMessageBody();
                }           

                ForwardToEmail(messageBody);
                extras = null;
                messageBody = null;
            }            
                // Display SMS message
    }

Again this works great on my Samsung, but not at all on the HTC. I have also checked for other SMS applications on the phone (ex. gosms pro, etc). I do not see anything on there that has a higher prioity (well I cheated for now and put mine to the highest possible - usually it is only at 999). Any insight or direction would be swell :).

2条回答
Melony?
2楼-- · 2019-08-03 05:09

To previous answer... As you can see here https://stackoverflow.com/questions/19565172/the-different-behavior-of-the-service-program-on-the-htc-one-evo-3d-and-samsu it is possible to avoid starting of Activity to start BroadcastReceiver. I see that it does not depends on Android version but evidently it really can work unpredictable on different devices such as HTC and Samsung...

查看更多
smile是对你的礼貌
3楼-- · 2019-08-03 05:10

Since Android 3.1 BroadcastReceivers wont be registered to the system until a component from your package has been launched.

So you need to create an Activity that will be launched on install. Starting this 'component' will register your manifest declared BroadcastReceivers

查看更多
登录 后发表回答