summaryrefslogtreecommitdiff
path: root/samples/ApiDemos/src/com/example/android/apis/os/SmsMessageReceiver.java
diff options
context:
space:
mode:
authorRoman Nurik <romannurik@google.com>2010-02-22 14:59:11 -0800
committerRoman Nurik <romannurik@google.com>2010-03-01 10:57:37 -0800
commitf096f961ffe5c62e0806b51f8feeb19ca9cdae60 (patch)
treef8ffec7e88739dd820a735b13b314f53040628a9 /samples/ApiDemos/src/com/example/android/apis/os/SmsMessageReceiver.java
parent7b23f86bfe1adb374b65154e8b0935a865e05d3b (diff)
Add SMS manager demo
Diffstat (limited to 'samples/ApiDemos/src/com/example/android/apis/os/SmsMessageReceiver.java')
-rw-r--r--samples/ApiDemos/src/com/example/android/apis/os/SmsMessageReceiver.java78
1 files changed, 78 insertions, 0 deletions
diff --git a/samples/ApiDemos/src/com/example/android/apis/os/SmsMessageReceiver.java b/samples/ApiDemos/src/com/example/android/apis/os/SmsMessageReceiver.java
new file mode 100644
index 000000000..ec809557f
--- /dev/null
+++ b/samples/ApiDemos/src/com/example/android/apis/os/SmsMessageReceiver.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.example.android.apis.os;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.database.Cursor;
+import android.net.Uri;
+import android.os.Bundle;
+import android.provider.ContactsContract;
+import android.telephony.SmsMessage;
+
+public class SmsMessageReceiver extends BroadcastReceiver {
+ /** Tag string for our debug logs */
+ private static final String TAG = "SmsMessageReceiver";
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ Bundle extras = intent.getExtras();
+ if (extras == null)
+ return;
+
+ Object[] pdus = (Object[]) extras.get("pdus");
+
+ for (int i = 0; i < pdus.length; i++) {
+ SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
+ String fromAddress = message.getOriginatingAddress();
+ String fromDisplayName = fromAddress;
+
+ Uri uri;
+ String[] projection;
+
+ // If targeting Donut or below, use
+ // Contacts.Phones.CONTENT_FILTER_URL and
+ // Contacts.Phones.DISPLAY_NAME
+ uri = Uri.withAppendedPath(
+ ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
+ Uri.encode(fromAddress));
+ projection = new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME };
+
+ // Query the filter URI
+ Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
+ if (cursor != null) {
+ if (cursor.moveToFirst())
+ fromDisplayName = cursor.getString(0);
+
+ cursor.close();
+ }
+
+ // Trigger the main activity to fire up a dialog that shows/reads the received messages
+ Intent di = new Intent();
+ di.setClass(context, SmsReceivedDialog.class);
+ di.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
+ di.putExtra(SmsReceivedDialog.SMS_FROM_ADDRESS_EXTRA, fromAddress);
+ di.putExtra(SmsReceivedDialog.SMS_FROM_DISPLAY_NAME_EXTRA, fromDisplayName);
+ di.putExtra(SmsReceivedDialog.SMS_MESSAGE_EXTRA, message.getMessageBody().toString());
+ context.startActivity(di);
+
+ // For the purposes of this demo, we'll only handle the first received message.
+ break;
+ }
+ }
+}