简体   繁体   English

BroadcastReceiver的onReceive不会在传入的短信上触发

[英]BroadcastReceiver's onReceive not firing on incoming sms

I am stuck for more than 6 hours, I have gone through every possible helpful post but no luck 我被困了6个多小时,我经历了所有有用的帖子,但没有运气

The onReceive() method of BroadcastReceiver is not getting fired. BroadcastReceiveronReceive()方法没有被触发。

Below is the code of my MainAcitivty , Manifest and BroadCastReceiver . 下面是我的代码MainAcitivtyManifestBroadCastReceiver

This is code of my mainactivity 这是我的主要活动代码

package com.example.luckydraw;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }



}

This is My broadcast reciever 这是我的广播接收者

package com.example.luckydraw;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class Smsrcv extends BroadcastReceiver {

     private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
     private static final String TAG = "Smsrcv";


    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub

        Toast.makeText(context, "I re", Toast.LENGTH_LONG).show();
          Log.i(TAG, "Intent recieved: " + intent.getAction());

            String message="test message";
        String PhoneNumber="03214304743";
            SmsManager.getDefault().sendTextMessage(PhoneNumber, null, message, null, null);


             if (intent.getAction() == 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.i(TAG, "Message recieved: " + messages[0].getMessageBody());
                     }
                 }
             }

    }

}

And finally this is my manifest 最后这是我的清单

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.luckydraw"
    android:versionCode="1"
    android:versionName="1.0" >


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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


        <receiver
            android:name=".Smsrcv"
            android:enabled="true"
            android:exported="true"
            >
            <intent-filter android:priority="1000" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

    </application>



</manifest>

There are certain messaging applications eg GO SMS PRO etc which have broadcast recievers' intent fiters like 有某些消息传递应用程序,例如GO SMS PRO等,具有广播接收者的意图拟合者,例如

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

Here android:priority is 2147483647 android:priority2147483647

This wasnt letting my app to catch System's broadcast !! 这并没有让我的应用程序捕获系统的广播!

If you want to check all messaging applications' priorities on your device 如果要检查设备上所有消息传递应用程序的优先级

use this 用这个

            Intent intent = new Intent("android.provider.Telephony.SMS_RECEIVED");
            List<ResolveInfo> infos = getPackageManager().queryBroadcastReceivers(intent, 0);
            for (ResolveInfo info : infos) {
              System.out.println("Receiver name:" + info.activityInfo.name + ";     priority=" + info.priority);
            }

SOLUTION

You should uninstall those with priority > 1000, to make your app work.. 您应该卸载优先级> 1000的程序,以使您的应用正常工作。

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

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