Android 5.0.2-SMS广播接收器-无法正常工作 [英] Android 5.0.2 - SMS Broadcast Receiver - can not get it working

查看:84
本文介绍了Android 5.0.2-SMS广播接收器-无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在小米Redmi Note 3上进行测试,我需要的非常简单: *注册广播接收者以接收传入的短信 *收到消息后,只需阅读即可

I am testing on Xiaomi Redmi Note 3 and what I need is very simple: * Register broadcast receiver for incoming text messages * Once message comes in, just read it

无论我如何尝试,似乎都无法获得接收者注册.

It looks like I can not get receiver register no matter what I try.

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

From the google docs, since 4.4 there should be no way for any app to swallow the event and every app listening should get a chance to get the event.

我尝试了各种组合,并用Google搜索了几乎所有内容.可能是小米手机问题吗?

I have tried all kind of combinations and googled pretty much everything. Could it be the Xiaomi phone issue?

这是我的清单:

<?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());
                }
            }
        }
    }
}

推荐答案

看起来小米拥有一个可控制几乎所有内容的安全应用程序.参见此处的另一个问题和答案

Looks like Xiaomi has a Security application that controls pretty much everything. See another question and answer here

步骤:

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

或者:

  • 转到安全"应用
  • 点击权限
  • 选择自动启动"或权限",然后启用应用程序所需的任何功能

这篇关于Android 5.0.2-SMS广播接收器-无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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