|
Cyril Sermon (@admin) |
The best practice is to create custom actions to use when broadcasting an Intent to a remote device, such as the one shown in the snippet below:
public static final String ACTION_OTA_ELIMINATE = “com.paad.ota_eliminate_action”;
The next snippet shows how to create a simple Intent that will be packaged within a data message to transmit the above action to a remote device:
Intent intent = new Intent(ACTION_OTA_ELIMINATE);
As with normal broadcast Intents, you can package additional information within the Intent using the extras Bundle. These extras will be included in the Intent when it’s re-broadcast on the remote device.
intent.putExtra(“long”, String.valueOf(location.getLatitude())); intent.putExtra(“lat”, String.valueOf(location.getLatitude())); intent.putExtra(“target”, “Sarah Conner”); intent.putExtra(“sender”, gTalk.getUsername());
Only String extras are currently supported in the OTA Intent broadcast mechanism. Non-string extras will be disregarded before transmission and won’t be available on the target device.
Send the message using the sendDataMessage method, passing in the target username and the Intent to broadcast. The sendDataMessage is available on IM Session or Chat Session objects, as shown below:
String username = “[email protected]”;
Send to target user.
imSession.sendDataMessage(username, intent);
Send to all chat room participants.
chatSession.sendDataMessage(intent);
To listen for data messages, register a Broadcast Receiver that filters on the action String included in a transmitted Intent.
GTalk data messages are processed as normal broadcast Intents, so they have no sender information asso-ciated when they’re received by a Broadcast Receiver. If you require such metadata, you should include them in the extras Bundle of the source Intent as was done in the code shown in the previous section.
The following skeleton code shows how to register a simple Broadcast Receiver implementation that can handle the Intent transmitted in the previous example:
BroadcastReceiver otaGTalkIntentReceiver = new BroadcastReceiver() { @Override
public void onReceive(Context _context, Intent _intent) { if (_intent.getAction().equals(ACTION_OTA_ELIMINATE)) { String sender = _intent.getStringExtra(“sender”); String target = _intent.getStringExtra(“target”);
String lat = _intent.getStringExtra(“lat”);
String lng = _intent.getStringExtra(“long”);
Location location = new Location(LocationManager.GPS_PROVIDER); location.setLatitude(Double.parseDouble(lat)); location.setLongitude(Double.parseDouble(lng));
// TODO: Do something with the data transmitted.
}
}
};
IntentFilter filter = new IntentFilter(ACTION_OTA_ELIMINATE); registerReceiver(otaGTalkIntentReceiver, filter);
If you own a mobile phone that’s less than two decades old, chances are you’re familiar with SMS mes-saging. SMS (short messaging service) is now one of the most-used features on mobile phones, with many people favoring it over making phone calls.
SMS technology is designed to send short text messages between mobile phones. It provides support for sending both text messages (designed to be read by people) and data messages (meant to be consumed by applications).
As a mature mobile technology, there’s a lot of information out there that describes the technical details of how an SMS message is constructed and transmitted over the air. Rather than rehash that here, the following sections focus on the practicalities of sending and receiving text and data messages within Android.
Android offers full access to SMS functionality from within your applications with the SMSManager. Using the SMS Manager, you can replace the native SMS application or create new applications that send text messages, react to incoming texts, or use SMS as a data transport layer.
SMS message delivery is not timely, so SMS is not really suitable for anything that requires real-time responsiveness. That said, the widespread adoption and resiliency of SMS networks make it a particularly good tool for delivering content to non-Android users and reducing the dependency on third-party servers.
As a ubiquitous technology, SMS offers a mechanism you can use to send text messages to other mobile phone users, irrespective of whether they have Android phones.
Compared to the instant messaging mechanism available through the GTalk Service, using SMS to pass data messages between applications is slow, possibly expensive, and suffers from high latency. On the other hand, SMS is supported by almost every phone on the planet, so where latency is not an issue, and updates are infrequent, SMS data messages are an excellent alternative.
SMS messaging in Android is handled by the SmsManager. You can get a reference to the SMS Manager using the static method SmsManger.getDefault, as shown in the snippet below.
SmsManager smsManager = SmsManager.getDefault();
To send SMS messages, your applications require the SEND_SMS permission. To request this permission, add it to the manifest using a uses-permission tag, as shown below:
<uses-permission android:name=”android.permission.SEND_SMS”/>
To send a text message, use sendTextMessage from the SMS Manager, passing in the address (phone number) of your recipient and the text message you want to send, as shown in the snippet below:
String sendTo = “5551234”;
String myMessage = “Android supports programmatic SMS messaging!”;
smsManager.sendTextMessage(sendTo, null, myMessage, null, null);
The second parameter can be used to specify the SMS service center to use; entering null as shown in the previous snippet uses the default service center for your carrier.
The final two parameters let you specify Intents to track the transmission and successful delivery of your messages.
To react to these Intents, create and register Broadcast Receivers as shown in the next section.
To track the transmission and delivery success of your outgoing SMS messages, implement and register Broadcast Receivers that listen for the actions you specify when creating the Pending Intents you pass in to the sendTextMessage method.
The first Pending Intent parameter, sentIntent, is fired when the message is either successfully sent or fails to send. The result code for the Broadcast Receiver that receives this Intent will be one of:
❑Activity.RESULT_OK To indicate a successful transmission.
SmsManager.RESULT_ERROR_GENERIC_FAILURE To indicate a nonspecific failure.
SmsManager.RESULT_ERROR_RADIO_OFF When the connection radio is turned off.
SmsManager.RESULT_ERROR_NULL_PDU To indicate a PDU failure.
The second Pending Intent parameter, deliveryIntent, is fired only after the destination recipient receives your SMS message.
The following code snippet shows a typical pattern for sending an SMS and monitoring the success of its transmission and delivery:
String SENT_SMS_ACTION = “SENT_SMS_ACTION”;
String DELIVERED_SMS_ACTION = “DELIVERED_SMS_ACTION”;
// Create the sentIntent parameter
Intent sentIntent = new Intent(SENT_SMS_ACTION);
PendingIntent sentPI = PendingIntent.getBroadcast(getApplicationContext(), 0,
sentIntent,
0);
// Create the deliveryIntent parameter
Intent deliveryIntent = new Intent(DELIVERED_SMS_ACTION);
PendingIntent deliverPI = PendingIntent.getBroadcast(getApplicationContext(), 0,
deliveryIntent,
0);
Register the Broadcast Receivers registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) { switch (getResultCode()) {
case Activity.RESULT_OK:
[… send success actions … ]; break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
[… generic failure actions … ]; break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
[… radio off failure actions … ]; break;
case SmsManager.RESULT_ERROR_NULL_PDU:
[… null PDU failure actions … ]; break;
}
}
},
new IntentFilter(SENT_SMS_ACTION));
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context _context, Intent _intent) { [… SMS delivered actions … ]
}
},
new IntentFilter(DELIVERED_SMS_ACTION));
// Send the message
smsManager.sendTextMessage(sendTo, null, myMessage, sentPI, deliverPI);