Android的PendingIntent演员,而不是由BroadcastReceiver的收是由、而不、演员、Android

2023-09-12 00:17:48 作者:对你的爱、不再有

当我路过群众演员到一个PendingIntent,永远不会被sendBroadcastReceiver收到的消息,因为这确实是的onReceive()方法的BroadcastReceiver 永远不会被调用。为什么会出现这种情况?

 公共类MainActivity延伸活动{
    私有静态字符串SENT =SMS_SENT;
    私有静态字符串DELIVERED =SMS_DELIVERED;
    私有静态诠释MAX_SMS_MESSAGE_LENGTH = 160;

    私有静态语境mContext;
    私人BroadcastReceiver的sendBroadcastReceiver,deliveryBroadcastReceiver;
    私人最终字符串DEBUG_TAG =的getClass()getSimpleName()的toString()。

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        // ---当短信已发送---
        sendBroadcastReceiver =新的BroadcastReceiver(){

            公共无效的onReceive(背景为arg0,意图ARG1){
                开关(的getResult code()){
                    案例Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(),SMS发送,Toast.LENGTH_SHORT).show();
                        打破;
                    案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(),一般故障,Toast.LENGTH_SHORT).show();
                        打破;
                    案例SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(),无服务,Toast.LENGTH_SHORT).show();
                        打破;
                    案例SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(),空的PDU,Toast.LENGTH_SHORT).show();
                        打破;
                    案例SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(),无线电关,Toast.LENGTH_SHORT).show();
                        打破;
                    }
            }
        };
        // ---当SMS已交付---
        deliveryBroadcastReceiver =新的BroadcastReceiver(){
            公共无效的onReceive(背景为arg0,意图ARG1){
                开关(的getResult code()){
                    案例Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(),SMS递送,Toast.LENGTH_SHORT).show();
                        打破;
                    案例Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(),短信未交付,Toast.LENGTH_SHORT).show();
                        打破;
                }
            }
        };

        Log.d(DEBUG_TAG的onCreate():注册接收器);
        registerReceiver(deliveryBroadcastReceiver,新的IntentFilter(交付));
        registerReceiver(sendBroadcastReceiver,新的IntentFilter(发送));

        mContext = getApplicationContext();

        的setContentView(R.layout.main);
        // ...绑定视图和设置的onClick听众(例如,一种叫sendSMS())

    }

    @覆盖
    保护无效的onDestroy(){
        //注销接收器
        Log.d(DEBUG_TAG的onDestroy():注销接收器);
        如果(sendBroadcastReceiver!= NULL){
            unregisterReceiver(sendBroadcastReceiver);
            sendBroadcastReceiver = NULL;
        }
        如果(deliveryBroadcastReceiver!= NULL){
            unregisterReceiver(deliveryBroadcastReceiver);
            deliveryBroadcastReceiver = NULL;
        }
        super.onDestroy();
    }

    // ---发送SMS消息到另一装置---
    公共静态无效sendSMS(字符串phoneNumber的,字符串消息){
        意图sendIntent =新的意图(发送);
        sendIntent.putExtra(extra_key,extra_value));

        PendingIntent piSent = PendingIntent.getBroadcast(mContext,0,sendIntent,PendingIntent.FLAG_UPDATE_CURRENT);
        PendingIntent piDelivered = PendingIntent.getBroadcast(mContext,0,新的意向(送到),0);
        SmsManager的SmsManager的= SmsManager.getDefault();

        INT长度= message.length();
        如果(长度> MAX_SMS_MESSAGE_LENGTH){
            ArrayList的<字符串>将MessageList = smsManager.divideMessage(消息);
            smsManager.sendMultipartTextMessage(phoneNumber的,空,将MessageList,NULL,NULL);
        } 其他
            smsManager.sendTextMessage(phoneNumber的,空,消息,piSent,piDelivered);
        }
   }

   // MainActivity更多方法...

}
 

解决方案

我想有与注册和注销的code的一些问题。相反,我注册了两个 BroadcastReceivers 的Andr​​oidManifest.xml 文件,并能顺利通过临时演员。

AndroidManifest.xml中

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android包=com.myexample安卓版code =1机器人:VERSIONNAME =1.0&GT ;
    <使用-SDK安卓的minSdkVersion =16机器人:targetSdkVersion =17/>
    <使用-权限的Andr​​oid:名称=android.permission.READ_CONTACTS/>
    <使用-权限的Andr​​oid:名称=android.permission.READ_PHONE_STATE/>
    <使用-权限的Andr​​oid:名称=android.permission.SEND_SMS/>
    <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_SMS/>
    <使用-权限的Andr​​oid:名称=android.permission.READ_SMS/>
    <使用-权限的Andr​​oid:名称=android.permission.WRITE_SMS/>
    <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_MMS/>
    <使用-权限的Andr​​oid:名称=android.permission.WRITE/>
    <使用-权限的Andr​​oid:名称=android.permission.VIBRATE/>
    <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
    <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
    <应用机器人:可调试=真正的机器人:图标=@可绘制/ ic_launcher_icon
机器人:标签=@字符串/ APP_NAME>
        <活动//主要活动...
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动//活动2 ...< /活性GT; //更多acitivies ...

        //发送接收器
        <接收器的Andr​​oid版本:NAME =com.sendit.receivers.SendBroadcastReceiver>
            <意向滤光器>
                <作用机器人:名称=SMS_SENT/>
            &所述; /意图滤光器>
        < /接收器>
        //传递接收器
        <接收器的Andr​​oid版本:NAME =com.sendit.receivers.DeliveryBroadcastReceiver>
            <意向滤光器>
                <作用机器人:名称=SMS_DELIVERED/>
            &所述; /意图滤光器>
        < /接收器>
    < /用途>
< /舱单>
 
android Notification的使用

SendBroadcastReceiver.java

 公共类SendBroadcastReceiver扩展的BroadcastReceiver {
    私人最终字符串DEBUG_TAG =的getClass()getSimpleName()的toString()。
    私有静态最后弦乐ACTION_SMS_SENT =SMS_SENT;

    //当SMS已发送
    公共无效的onReceive(上下文的背景下,意图意图){

        串动= intent.getAction();

        如果(action.equals(ACTION_SMS_SENT)){

            开关(的getResult code()){
                案例Activity.RESULT_OK:
                    Toast.makeText(背景下,短信发送,Toast.LENGTH_SHORT).show();
                    叠B = intent.getExtras();
                    Log.d(DEBUG_TAG,sendBroadcastReceiver:b为+ B);
                    如果(B!= NULL){
                        字符串值= b.getString(extra_key);
                        Log.d(DEBUG_TAG,sendBroadcastReceiver:值+值);
                    }
                    打破;
                案例SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                    Toast.makeText(背景下,一般故障,Toast.LENGTH_SHORT)
                        。显示();
                    打破;
                案例SmsManager.RESULT_ERROR_NO_SERVICE:
                    Toast.makeText(背景下,无服务,Toast.LENGTH_SHORT)
                        。显示();
                    打破;
                案例SmsManager.RESULT_ERROR_NULL_PDU:
                    Toast.makeText(上下文,空的PDU,Toast.LENGTH_SHORT).show();
                    打破;
                案例SmsManager.RESULT_ERROR_RADIO_OFF:
                    Toast.makeText(背景下,无线关,Toast.LENGTH_SHORT).show();
                    打破;
            }

        }
    }

}
 

DeliveryBroadcastReceiver.java

 公共类DeliveryBroadcastReceiver扩展的BroadcastReceiver {
    私人最终字符串DEBUG_TAG =的getClass()getSimpleName()的toString()。
    私有静态最后弦乐ACTION_SMS_DELIVERED =SMS_DELIVERED;

    //当SMS已交付
    公共无效的onReceive(上下文的背景下,意图意图){

        串动= intent.getAction();

        如果(action.equals(ACTION_SMS_DELIVERED)){

            开关(的getResult code()){
                案例Activity.RESULT_OK:
                    Toast.makeText(背景下,短信交付,
                    Toast.LENGTH_SHORT).show();
                    打破;
                案例Activity.RESULT_CANCELED:
                    Toast.makeText(背景下,短信未交付,
                    Toast.LENGTH_SHORT).show();
                    打破;
            }
        }
    }

}
 

MainActivity.java

 公共类MainActivity延伸活动{
    私有静态字符串ACTION_SMS_SENT =SMS_SENT;
    私有静态字符串ACTION_SMS_DELIVERED =SMS_DELIVERED;
    私有静态诠释MAX_SMS_MESSAGE_LENGTH = 160;

    私有静态语境mContext;
    私人最终字符串DEBUG_TAG =的getClass()getSimpleName()的toString()。

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        mContext = getApplicationContext();

        的setContentView(R.layout.main);
        // ...绑定视图和设置的onClick听众(例如,一种叫sendSMS())

    }

    // ---发送SMS消息到另一装置---
    公共静态无效sendSMS(字符串phoneNumber的,字符串消息){
        意图sendIntent =新的意图(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,新意图(ACTION_SMS_DELIVERED),0);
        SmsManager的SmsManager的= SmsManager.getDefault();

        INT长度= message.length();
        如果(长度> MAX_SMS_MESSAGE_LENGTH){
            ArrayList的<字符串>将MessageList = smsManager.divideMessage(消息);
            smsManager.sendMultipartTextMessage(phoneNumber的,空,将MessageList,NULL,NULL);
        } 其他
            smsManager.sendTextMessage(phoneNumber的,空,消息,piSent,piDelivered);
        }
    }

    // MainActivity更多方法...

}
 

When I pass extras to a PendingIntent, the message is never received by the sendBroadcastReceiver, because the onReceive() method of this BroadcastReceiver is never called. Why is this happening?

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

}

解决方案

I guess there are some issues with registering and unregistering in the code. Instead I registered the two BroadcastReceivers in the AndroidManifest.xml file and was able to successfully pass extras.

AndroidManifest.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>

SendBroadcastReceiver.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 ... 

}

 
精彩推荐
图片推荐