短信广播接收器无法正常工作 [英] SMS Broadcast Receiver not working

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

问题描述

好吧,我已经在Stack上尝试了所有解决方案,但没有任何工作。我当前的方法从MainActivity注册SmsListener接收器。我要做的就是初始化onReceive方法。没有错误;它根本就没有接收广播。我究竟做错了什么?在此处粘贴适用的代码。还有其他任何可能需要的东西请你问。

Alright, I have tried every solution on Stack and nothing works.My current method registers the "SmsListener" receiver from the MainActivity. All I'm trying to do is initialize the onReceive method. 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.

更新:
这是一个类似的未解决的问题
在Google环聊或其他应用收到后收听Android来电短信
我在Android 6.0.1下测试。目标Sdk版本是22. Min Sdk是19.值得注意的是,我刚刚使用Android 4.4.2在LG Optimus GPro上测试了我的原始代码并且它有效。它仍然无法在我的Nexus上运行Android 6.0.1。

清单:

<?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>

主要活动

    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

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");

}}


推荐答案

找到一个解决方案。

首先将另一个应用程序设为您的默认短信应用程序。

First make another app your default SMS app.

然后:Google环聊 - >设置(禁用合并对话) - >短信(禁用短信)

Then: Google Hangout --> Settings (Disable merged conversations) --> SMS (Disable SMS)

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

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