我正试图使用以下代码以编程方式在活动上注册广播接收器

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

当收到新短信时,我的应用程序不会收到任何通知。我正在使用android 4.2和hangout和gosms
更新2:
卸载gosms后,它仍在工作。但我希望接收sms_received intent操作,即使用户是否安装了gosms应用程序。是否可能?有别的解决办法吗?

最佳答案

您还需要实现mms接收器+headlessmssendservice+composesmsactive
有关详细信息,请参见链接:
http://android-developers.blogspot.fr/2013/10/getting-your-sms-apps-ready-for-kitkat.html

07-28 01:45