-1

我被困了6个多小时,我浏览了所有可能有用的帖子,但没有运气

onReceive()方法BroadcastReceiver不会被解雇。

下面是我的代码MainAcitivtyManifestBroadCastReceiver

这是我的主要活动的代码

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

    }

}

最后这是我的清单

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

1 回答 1

4

有某些消息应用程序,例如 GO SMS PRO 等,它们具有广播接收者的意图过滤器,例如

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

这里android:priority2147483647

这并没有让我的应用程序捕捉系统的广播!

如果您想检查设备上所有消息传递应用程序的优先级

用这个

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

解决方案

您应该卸载优先级 > 1000 的那些,以使您的应用程序正常工作。

于 2013-04-22T14:20:08.507 回答