BroadcastReceiver的onReceive没有触发传入的短信

时间:2013-04-18 21:35:08

标签: android broadcastreceiver android-broadcast

我被困了6个多小时,我已经完成了所有可能有用的帖子但没有运气

onReceive()的{​​{1}}方法未被解雇。

以下是我的BroadcastReceiverMainAcitivtyManifest的代码。

这是我的主要活动代码

BroadCastReceiver

这是我的广播接收器

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



}

最后这是我的清单

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

    }

}

1 个答案:

答案 0 :(得分:4)

某些消息传递应用程序,例如GO短信PRO等有广播接收者的意图fiters像

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

此处 android:priority 2147483647

这不是让我的应用程序能够捕获系统的广播!!

如果要在设备上检查所有邮件应用程序的优先级

使用此

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

<强>解

您应该卸载优先级为&gt; 1000,让你的应用程序工作..