当我向PendingIntent传递extras时,sendBroadcastReceiver从未收到消息,因为从未调用此BroadcastReceiver
的onReceive()
方法。
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 ...
}
2条答案
按热度按时间nmpmafwu1#
我猜在代码中注册和取消注册有一些问题,相反,我在
AndroidManifest.xml
文件中注册了两个BroadcastReceivers
,并且能够成功地通过额外的操作。安卓清单.xml
发送广播接收器.java
传送广播接收器.java
主要活动.java
xoshrz7s2#
广播接收器中的任何额外内容为空:
您应该使用
FLAG_MUTABLE
下面是一个例子: