Android 5.0.2 - 短信广播接收器 - 无法正常工作

Android 5.0.2 - SMS Broadcast Receiver - can not get it working

我在小米红米Note 3上测试,需要的东西很简单: * 为传入的短信注册广播接收器 * 收到消息后,请阅读

看来无论我怎么尝试都无法获得接收者注册。

根据 google 文档,从 4.4 开始,任何应用程序都应该无法吞下该事件,并且每个监听的应用程序都应该有机会获得该事件。

我已经尝试了所有类型的组合并且 googled 几乎所有的东西。难道是小米phone的问题?

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.com.dimitar.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="21" />

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <receiver
            android:name="com.example.com.dimitar.test.SmsListener"
            android:enabled="true" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Java代码:

 package com.example.com.dimitar.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SmsListener extends BroadcastReceiver{

    private SharedPreferences preferences;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Bundle bundle = intent.getExtras();

         Toast toast = Toast.makeText(context, "poruka: ", Toast.LENGTH_SHORT);
        toast.show();

        if(bundle != null){
            //---get the SMS message passed in---
            SmsMessage[] msgs = null;
            String msg_from;
            if (bundle != null){
                //---retrieve the SMS message received---
                try{
                    Object[] pdus = (Object[]) bundle.get("pdus");
                    msgs = new SmsMessage[pdus.length];
                    for(int i=0; i<msgs.length; i++){
                        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                        msg_from = msgs[i].getOriginatingAddress();
                        String msgBody = msgs[i].getMessageBody();


                        Toast toast1 = Toast.makeText(context, "poruka: " + msgBody, Toast.LENGTH_SHORT);
                        toast1.show();
                    }
                }catch(Exception e){
//                            Log.d("Exception caught",e.getMessage());
                }
            }
        }
    }
}

看起来小米有一个安全应用程序可以控制几乎所有的东西。参见

步骤:

  • 转到设置 > 已安装的应用程序
  • 找到该应用 > 点击它
  • 转到权限管理器并启用您需要的权限

或者:

  • 转到安全应用程序
  • 点击权限
  • 选择自动启动或权限并启用您的应用程序所需的任何内容