我无法让SMS广播接收器在HTC One上的Android 4.1.3上运行 [英] I am unable to get a SMS Broadcast Receiver to work on Android 4.1.3 on HTC one

查看:72
本文介绍了我无法让SMS广播接收器在HTC One上的Android 4.1.3上运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的。我正在研究一个简单的SMS接收器,该接收器可以捕获传入的文本消息,对它们进行一些逻辑处理,然后将其转发到我的电子邮件中以进行更永久的存储。广播接收器已注册在我的清单中,并且在我在Galaxy S3上进行测试时,该代码非常有效。但是,当我将其安装在HTC One上时,似乎从未触发过Boradcast接收器。只是想知道是否有人在HTC上遇到过这种情况,以及是否有建议采取的措施。

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.

注意:
我已经在网上尝试了所有常见的答案(提高优先级,确保所有权限都在清单中,请在活动中进行尝试,等等)。该目录似乎也没有帮助。如果我在onReceive()方法的开头设置断点或在那做烤面包,那么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.

这是我的清单。 :

<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>

和接收它的我的代码:

    @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
    }

再次在我的三星上效果很好,但在HTC上却一点也不好。我还检查了手机上的其他SMS应用程序(例如gosms pro等)。我在那儿看不到任何东西有更高的自尊心(我现在作弊了,把我的价格尽可能地高了-通常只有999)。
任何见识或方向都会膨胀:)。

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 :).

推荐答案

自Android 3.1起, BroadcastReceivers 在启动软件包中的组件之前不会注册到系统。

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

因此,您需要创建活动将在安装时启动。启动此组件将注册声明为 BroadcastReceivers

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

这篇关于我无法让SMS广播接收器在HTC One上的Android 4.1.3上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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