BroadcastReceiver不适用于SMS_RECEIVED

时间:2014-02-09 14:18:22

标签: android android-intent sms broadcastreceiver intentfilter

我正在尝试使用此代码以编程方式在活动上注册BroadcastReceiver

br=new BroadcastReceiver() {                        
            private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
            private static final String TAG = "cyb";

        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "ok", Toast.LENGTH_LONG).show();
             Log.e(TAG, "Intent recieved: " + intent.getAction());

                if (intent.getAction().equals(SMS_RECEIVED)) {
                    Bundle bundle = intent.getExtras();
                    if (bundle != null) {
                        Object[] pdus = (Object[])bundle.get("pdus");
                        final SmsMessage[] messages = new SmsMessage[pdus.length];
                        for (int i = 0; i < pdus.length; i++) {
                            messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        }
                        if (messages.length > -1) {
                            Log.e(TAG, "Message recieved: " + messages[0].getMessageBody());
                        }
                    }
                }
        }
    };      

    IntentFilter inf=new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
    inf.setPriority(999);
    registerReceiver(br, inf);

我还将此权限添加到AndroidManifest.xml

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

我的应用在收到新短信后不会收到任何通知。我正在使用带有环聊和GoSMS的Android 4.2

更新2:

卸载GOSMS之后它正在工作。但我想接收SMS_RECEIVED意图动作,即使用户安装了GoSMS应用程序。是否可能?任何替代解决方案?

1 个答案:

答案 0 :(得分:0)

您还需要实现MMS接收器+ HeadlessSmsSendService + ComposeSmsActivity

请参阅链接了解详情: http://android-developers.blogspot.fr/2013/10/getting-your-sms-apps-ready-for-kitkat.html