Когда я передаю дополнительные функции в PendingIntent, сообщение никогда не будет получено sendBroadcastReceiver, потому что метод onReceive()
этого BroadcastReceiver
никогда не вызывается. Почему это происходит?
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 ...
}