当前位置: 首页 > 知识库问答 >
问题:

Android PendingIntent附加功能,未被BroadcastReceiver接收

张啸
2023-03-14

当我将额外的内容传递给PendingIntent时,sendBroadcastRecencer永远不会接收到消息,因为从来没有调用过这个BroadcastRecenceronAccess()方法。为什么会发生这种情况?

public class MainActivity extends Activity {
    private static String SENT = "SMS_SENT";
    private static String DELIVERED = "SMS_DELIVERED";
    private static int MAX_SMS_MESSAGE_LENGTH = 160;

    private static Context mContext;
    private BroadcastReceiver sendBroadcastReceiver, deliveryBroadcastReceiver;
    private final String DEBUG_TAG = getClass().getSimpleName().toString();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // ---when the SMS has been sent---
        sendBroadcastReceiver = new BroadcastReceiver() {

            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS Sent", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", Toast.LENGTH_SHORT).show();
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", Toast.LENGTH_SHORT).show();
                        break;
                    }
            }
        };
        // ---when the SMS has been delivered---
        deliveryBroadcastReceiver = new BroadcastReceiver() {
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode()) {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS Delivered", Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered",Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        };

        Log.d(DEBUG_TAG, "onCreate() : Register receivers");
        registerReceiver(deliveryBroadcastReceiver, new IntentFilter(DELIVERED));
        registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));

        mContext = getApplicationContext();

        setContentView(R.layout.main);
        // ... Bind views and set up onClick listeners ( for example, one to call sendSMS() )

    }

    @Override
    protected void onDestroy() {
        // Unregister receivers
        Log.d(DEBUG_TAG, "onDestroy() : Unregister receivers");
        if (sendBroadcastReceiver != null) {
            unregisterReceiver(sendBroadcastReceiver);
            sendBroadcastReceiver = null;
        }
        if (deliveryBroadcastReceiver != null) {
            unregisterReceiver(deliveryBroadcastReceiver);
            deliveryBroadcastReceiver = null;
        }
        super.onDestroy();
    }

    // ---sends an SMS message to another device---
    public static void sendSMS(String phoneNumber, String message) {
        Intent sendIntent = new Intent(SENT);
        sendIntent.putExtra("extra_key", "extra_value"));

        PendingIntent piSent = PendingIntent.getBroadcast(mContext, 0, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent piDelivered = PendingIntent.getBroadcast(mContext, 0, new Intent(DELIVERED), 0);
        SmsManager smsManager = SmsManager.getDefault();

        int length = message.length();
        if (length > MAX_SMS_MESSAGE_LENGTH) {
            ArrayList < String > messagelist = smsManager.divideMessage(message);
            smsManager.sendMultipartTextMessage(phoneNumber, null, messagelist, null, null);
        } else 
            smsManager.sendTextMessage(phoneNumber, null, message, piSent, piDelivered);
        }
   }

   //More methods of MainActivity ... 

}

共有1个答案

满昊然
2023-03-14

我想在代码中注册和注销可能会有一些问题。相反,我在AndroidManifest中注册了两个BroadcastReceivers。xml文件,并且能够成功地传递额外信息。

Androidanifest.xml

<?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myexample" android:versionCode="1" android:versionName="1.0">
    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.WRITE_SMS" />
    <uses-permission android:name="android.permission.RECEIVE_MMS" />
    <uses-permission android:name="android.permission.WRITE" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application android:debuggable="true" android:icon="@drawable/ic_launcher_icon"
android:label="@string/app_name">
        <activity //Main activity... 
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity //Activity 2 ... </activity>//More acitivies ... 

        // Send Receiver
        <receiver android:name="com.sendit.receivers.SendBroadcastReceiver">
            <intent-filter>
                <action android:name="SMS_SENT" />
            </intent-filter>
        </receiver>
        //Delivery Receiver
        <receiver android:name="com.sendit.receivers.DeliveryBroadcastReceiver">
            <intent-filter>
                <action android:name="SMS_DELIVERED" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

发送广播接收器。JAVA

public class SendBroadcastReceiver extends BroadcastReceiver {
    private final String DEBUG_TAG = getClass().getSimpleName().toString();
    private static final String ACTION_SMS_SENT = "SMS_SENT";

    // When the SMS has been sent
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        if (action.equals(ACTION_SMS_SENT)) {

            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(context, "SMS Sent", Toast.LENGTH_SHORT).show();
                    Bundle b = intent.getExtras();
                    Log.d(DEBUG_TAG, "sendBroadcastReceiver : b is " + b);
                    if (b != null) {
                        String value = b.getString("extra_key");
                        Log.d(DEBUG_TAG, "sendBroadcastReceiver : value is " + value);
                    }
                    break;
                case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT)
                        .show();
                    break;
                case SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(context, "No service", Toast.LENGTH_SHORT)
                        .show();
                    break;
                case SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(context, "Null PDU", Toast.LENGTH_SHORT).show();
                    break;
                case SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(context, "Radio off", Toast.LENGTH_SHORT).show();
                    break;
            }

        }
    }

}

DeliveryBroadcastReceiver.java

public class DeliveryBroadcastReceiver extends BroadcastReceiver {
    private final String DEBUG_TAG = getClass().getSimpleName().toString();
    private static final String ACTION_SMS_DELIVERED = "SMS_DELIVERED";

    // When the SMS has been delivered
    public void onReceive(Context context, Intent intent) {

        String action = intent.getAction();

        if (action.equals(ACTION_SMS_DELIVERED)) {

            switch (getResultCode()) {
                case Activity.RESULT_OK:
                    Toast.makeText(context, "SMS Delivered",
                    Toast.LENGTH_SHORT).show();
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(context, "SMS not delivered",
                    Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    }

}

MainActivity.java

public class MainActivity extends Activity {
    private static String ACTION_SMS_SENT = "SMS_SENT";
    private static String ACTION_SMS_DELIVERED = "SMS_DELIVERED";
    private static int MAX_SMS_MESSAGE_LENGTH = 160;

    private static Context mContext;
    private final String DEBUG_TAG = getClass().getSimpleName().toString();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mContext = getApplicationContext();

        setContentView(R.layout.main);
        // ... Bind views and set up onClick listeners ( for example, one to call sendSMS() )

    }

    // ---sends an SMS message to another device---
    public static void sendSMS(String phoneNumber, String message) {
        Intent sendIntent = new Intent(ACTION_SMS_SENT);
        sendIntent.putExtra("extra_key", "extra_value"));

        PendingIntent piSent = PendingIntent.getBroadcast(mContext, 0, sendIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent piDelivered = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_SMS_DELIVERED), 0);
        SmsManager smsManager = SmsManager.getDefault();

        int length = message.length();
        if (length > MAX_SMS_MESSAGE_LENGTH) {
            ArrayList < String > messagelist = smsManager.divideMessage(message);
            smsManager.sendMultipartTextMessage(phoneNumber, null, messagelist, null, null);
        } else 
            smsManager.sendTextMessage(phoneNumber, null, message, piSent, piDelivered);
        }
    }

    //More methods of MainActivity ... 

}
 类似资料:
  • 根据你使用 Jekyll 的不同方式,Jekyll 允许你安装一些可选的附加功能。 数学支持 使用 Kramdown 时可以选择使用由 MathJax 提供的 LaTeX 格式到 PNG 格式的数学区块渲染器。具体细节可查阅 Kramdown 文档中的 math blocks (数学区块) 以及 math support (数学支持) 部分。 使用 MathJax 需要你设置引用相关的 JavaS

  • 问题内容: 我有一个定义如下的指令- 我遇到的问题是从未调用Link函数。谢谢! PS编译逻辑的原因是该指令是递归的。 问题答案: 如果您的意思是未调用,则很清楚:编译函数已返回链接函数。定义为不再重要,将被忽略。

  • Android清单的重要部分是: 为什么我的广播接收器没有收到意图?

  • 问题内容: 我对jQuery函数创建的新元素的“点击”操作有问题,它不起作用。 我为此链接创建了一个JSFiddle,但是它不能完美运行,因为我无法在JSFiddle中调用Ajax请求。 我有两个按钮 首次调用ajax函数时,一个按钮会回答以下输出: 按钮二仅用于擦除。按钮一创建。 当我单击此新按钮时,ajax函数将返回此按钮, 第一个按钮消失,第二个按钮回来。现在存在问题所在,如果我单击第一个按

  • 我正在开发的一个应用程序允许用户自己读取确认短信的内容,输入验证码。对于使用早于Oreo(API 26)的操作系统的所有设备,BroadcastReceiver的实现工作正常,并允许正确接收SMS。在这个实现中,我的意思是将receiver对象放置在AndroidManifest中。 但是,从Oreo开始,必须显式地将BroadcastReceivers注册到适当的上下文。我对此执行如下: 在接收

  • 问题内容: 在> iOS 5中可以使用吗? 我认为应该,但到目前为止还没有。 问题答案: 您可以使用单独的元素来解决此问题,并且确实有效! HTML: CSS: