简体   繁体   English

短信广播接收器不工作

[英]SMS Broadcast Receiver not working

Alright, I have tried every solution on Stack and nothing works.My current method registers the "SmsListener" receiver from the MainActivity.好吧,我已经尝试了 Stack 上的所有解决方案,但没有任何效果。我当前的方法是从 MainActivity 注册“SmsListener”接收器。 All I'm trying to do is initialize the onReceive method.我要做的就是初始化 onReceive 方法。 There are no errors;没有错误; It simply isn't picking up a broadcast.它只是不接收广播。 What am I doing wrong?我做错了什么? Pasting the applicable code here.在此处粘贴适用的代码。 Anything else that may be needed please just ask.任何其他可能需要的请询问。

Update: Here is a similar unresolved issue Listen Android incoming SMS when Google Hangout or other app receives it I am testing under Android 6.0.1.更新:这是一个类似的未解决的问题, 当 Google Hangout 或其他应用程序收到时,收听 Android 传入的 SMS我正在 Android 6.0.1 下进行测试。 Target Sdk version is 22. Min Sdk is 19. It's worth noting that I just tested my original code on an LG Optimus GPro with Android 4.4.2 and it worked.目标 Sdk 版本是 22。最小 Sdk 是 19。值得注意的是,我刚刚在带有 Android 4.4.2 的 LG Optimus GPro 上测试了我的原始代码并且它工作正常。 It still isn't working on my Nexus with Android 6.0.1.它仍然无法在我的 Nexus 上使用 Android 6.0.1。

Manifest:清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.biapps.makin_biscuits">
<uses-sdk android:minSdkVersion="4" />

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"/>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />

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

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action `android:name="android.service.notification.NotificationListenerService" />`
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".ContactsList">
        <intent-filter>
            <category android:name="android.intent.category.ALTERNATIVE" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".SmsListener"
        android:priority="999"
        android:enabled="true"
        android:exported="true">

    </receiver>

    <receiver
        android:name=".IncomingCallReceiver"
        android:enabled="true"
        android:exported="true">

    </receiver>


</application>

Main Activity主要活动

    package com.biapps.makin_biscuits;

    import android.service.notification.NotificationListenerService;
    import android.app.NotificationManager;

    import android.content.Context;

    import android.content.IntentFilter;
    import android.media.AudioManager;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.content.Intent;

    import android.view.View;
    import android.widget.ImageButton;

    import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    //set object labels and states here
    private ImageButton icon;
    private AudioManager am;
    private ImageButton people;
    private ImageButton ring;
    private NotificationManager nm;
    private NotificationListenerService nls;
    IncomingCallReceiver broadCastReceiver = new IncomingCallReceiver();
    SmsListener smsReceiver = new SmsListener();
    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

        icon = (ImageButton) findViewById(R.id.icon);
        icon.setOnClickListener(imgButtonHandler);
        people = (ImageButton) findViewById(R.id.people);
        //people.setOnClickListener(peopleButtonHandler);
        ring = (ImageButton) findViewById(R.id.ring);

    }

    int buttonstate = 0;
    public View.OnClickListener imgButtonHandler = new View.OnClickListener() {

        public void onClick(View v) {
            if (buttonstate == 0) {

                icon.setImageResource(R.drawable.buttonup);
                buttonstate = 1;
                am.setRingerMode(0);

                registerReceiver(broadCastReceiver, new IntentFilter(
                        "android.intent.action.PHONE_STATE"));
                registerReceiver(smsReceiver, new IntentFilter(
                        "android.intent.action.DATA_SMS_RECEIVED"));
                registerReceiver(smsReceiver, new IntentFilter(
                        "android.provider.Telephony.SMS_RECEIVED"));
                registerReceiver(smsReceiver, new IntentFilter(
                        "android.provider.Telephony.DATA_SMS_RECEIVED"));
                Toast.makeText(getApplicationContext(),"Diving!", `Toast.LENGTH_SHORT)`
                        .show();

            } else {

                icon.setImageResource(R.drawable.button);
                buttonstate = 0;
                am.setRingerMode(2);

                unregisterReceiver(broadCastReceiver);
                unregisterReceiver(smsReceiver);
                Toast.makeText(getApplicationContext(),"Surfacing!", Toast.LENGTH_SHORT)
                        .show();
            }
        }
    };}

SmsListener短信监听器

package com.biapps.makin_biscuits;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import android.telephony.TelephonyManager;


public class SmsListener extends BroadcastReceiver {

private static final String TAG = "SmsListener";

public static final String SMS_BUNDLE = "pdus";
@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "ON SMS RECEIVE BROADCAST", Toast.LENGTH_LONG).show();
    Log.i(TAG, "SmsListener - onReceiveCalled");

}}

Try following way with highest reading priority value,尝试遵循具有最高读取优先级值的方式,

<receiver android:name=".SmsListener"
             android:enabled="true"
            android:exported="true"
            android:permission="android.permission.READ_SMS">
    <intent-filter android:priority="2147483647">
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>

This will surely solve out your problem.这肯定会解决您的问题。

Update from below comment,从下面的评论更新,

Since you are checking on Android version 6.0.1 just follow these steps,由于您正在检查 Android 版本 6.0.1 只需按照以下步骤操作,

  1. Go to Settings,进入设置,
  2. Go to Apps转到应用程序
  3. Select your Application选择您的应用程序
  4. Choose Permissions Option选择权限选项
  5. Enable SMS permission启用短信权限

After spending more than an hour I found that RECEIVE_SMS permission is required.花了一个多小时后,我发现需要RECEIVE_SMS权限。

ActivityCompat.requestPermissions(this, 
            new String[]{Manifest.permission.RECEIVE_SMS},
            MY_PERMISSIONS_REQUEST_SMS_RECEIVE);

Prioirty is not required to be set.不需要设置优先级。 This should work.这应该有效。

You are registering broadcast your in your Activity , so it won't work if your app is in background.您正在Activity中注册broadcast ,因此如果您的应用程序在后台运行,它将无法工作。 you can remove this from your Activity and you can register it in Manifest.你可以从你的Activity删除它,你可以在清单中注册它。

ex:例如:

 <receiver android:name=".SmsListener">
    <intent-filter android:priority="999">
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>

Along with this add permission to recieve sms.与此一起添加接收短信的权限。

Try this, and it will work试试这个,它会起作用

Found a solution.找到了解决办法。

First make another app your default SMS app.首先将另一个应用程序设为您的默认短信应用程序。

Then: Google Hangout --> Settings (Disable merged conversations) --> SMS (Disable SMS)然后:谷歌环聊-->设置(禁用合并对话)-->短信(禁用短信)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM