Android:如何发送短消息&接收短信&获取短信,

・31 分钟阅读

下载&运行

你可以在下面下载eclipse或androidstudio的项目或预装二进制*.apk-files,以便在你的设备上安装和运行演示,并且确保它真的适用于你的手机 。

备注:所有APK文件均通过 Smali反汇编程序(用于符合实际源代码)进行安全检查。 没有病毒没有多余的权限。

内容

发送短信

我们可以用2种方式发送短信-使用Intent或不带 Intent 。

任何短消息发送都需要向清单添加android.permission.SEND_SMS权限,

<uses-permissionandroid:name="android.permission.SEND_SMS"/>

带有Intent(通过短信应用)的

直接通过内置的smsmms应用程序

此代码强制 Android SMSmms应用程序中内置的打开指定的电话号码 接下来,用户可以按"发送"按钮,并将发送短信。

要求android.permission.SEND_SMS权限(请参见上方)。

/* Phone Number is just all digits of the number (including country code)
 *"+" isn't needed, but can appear
 * For example:
 * I have Beeline Phone Number +7 (962) 000-00-00,
 * in my case, it should be 79620000000 or +79620000000 */strPhone = "XXXXXXXXXXX";
strMessage = "LoremnIpsum";
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.setType("vnd.android-dir/mms-sms");
sendIntent.putExtra("address", strPhone);
sendIntent.putExtra("sms_body", strMessage); 
startActivity(sendIntent);
通过默认应用程序它处理 Smsto: 协议

代码运行"smsto: XXXXXXXXXXX"url 。Android处理它,并且运行默认应用程序,该应用程序被设置为处理SmsTo协议。

默认情况下它是内置的Android SMS/MMS应用程序(请参见上面的直接 Intent )。 但是,如果我们安装任何能够处理SMS,并且将自身设置为smsto处理程序的第三方应用程序(例如, Skype我们第一次看到对话框,我们应该在内置应用程序和这个3 rd方应用程序之间进行选择 , 这很不方便。

要求android.permission.SEND_SMS权限(请参见上方)。

/* Phone Number is just all digits of the number (including country code)
 *"+" isn't recommended, because there it is already included into Uri (see below)
 * For example:
 * I have Beeline Phone Number +7 (962) 000-00-00,
 * in my case, it should be 79620000000 */strPhone = "XXXXXXXXXXX";
strMessage = "LoremnIpsum";
Uri sms_uri = Uri.parse("smsto:+" + strPhone); 
Intent sms_intent = new Intent(Intent.ACTION_SENDTO, sms_uri); 
sms_intent.putExtra("sms_body", txtMessage.getText().toString()); 
startActivity(sms_intent); 

没有Intent(无提示无用户界面)

SmsManager.sendTextMessage()

要求android.permission.SEND_SMS权限(请参见上方)。

/* Phone Number is just all digits of the number (including country code)
 *"+" isn't needed, but can appear
 * For example:
 * I have Beeline Phone Number +7 (962) 000-00-00,
 * in my case, it should be 79620000000 or +79620000000 */String strPhone = "XXXXXXXXXXX";String strMessage = "LoremnIpsum";
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(strPhone, null, strMessage, null, null);
Toast.makeText(this, "Sent.", Toast.LENGTH_SHORT).show();
SmsManager.sendMultipartTextMessage()- 如果SMS长于160个符号

如果短信的长度超过了某个限制(通常是160个符号),sendTextMessage()就不会发送它。
在这种情况下我们应该使用 sendMultipartTextMessage()

要求android.permission.SEND_SMS权限(请参见上方)。

String strPhone = "XXXXXXXXXXX";String strMessage = "12345678901234567890123456789012345678901234567890123456789012345678901234567890
 12345678901234567890123456789012345678901234567890123456789012345678901234567890TEST";
SmsManager sms = SmsManager.getDefault();
ArrayList<String> messageParts = sms.divideMessage(strMessage);
sms.sendMultipartTextMessage(strPhone, null, messageParts, null, null);
Toast.makeText(this, "Sent.", Toast.LENGTH_SHORT).show();
SendIntent & DeliveredIntent - 确保发送短信&已发送

要求android.permission.SEND_SMS权限(请参见上方)。

对于 sendTextMessage():

String strPhone = "XXXXXXXXXXX";String strMessage = "LoremnIpsum";
SmsManager sms = SmsManager.getDefault();/* ---- Preparing Intents To Check While Sms Sent & Delivered ---- */Context curContext = this.getApplicationContext();
PendingIntent sentPending = PendingIntent.getBroadcast(curContext,
 0, new Intent("SENT"), 0);
curContext.registerReceiver(new BroadcastReceiver() 
{
 @Overridepublicvoid onReceive(Context arg0, Intent arg1) 
 {
 switch (getResultCode()) {
 case Activity.RESULT_OK:
 Toast.makeText(getBaseContext(), "Sent.",
 Toast.LENGTH_LONG).show();
 break;
 case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
 Toast.makeText(getBaseContext(), "Not Sent: Generic failure.",
 Toast.LENGTH_LONG).show();
 break;
 case SmsManager.RESULT_ERROR_NO_SERVICE:
 Toast.makeText(getBaseContext(), "Not Sent: No service (possibly, no SIM-card).",
 Toast.LENGTH_LONG).show();
 break;
 case SmsManager.RESULT_ERROR_NULL_PDU:
 Toast.makeText(getBaseContext(), "Not Sent: Null PDU.",
 Toast.LENGTH_LONG).show();
 break;
 case SmsManager.RESULT_ERROR_RADIO_OFF:
 Toast.makeText(getBaseContext(), "Not Sent: Radio off (possibly, Airplane mode enabled in Settings).",
 Toast.LENGTH_LONG).show();
 break;
 }
 }
}, new IntentFilter("SENT"));
PendingIntent deliveredPending = PendingIntent.getBroadcast(curContext,
 0, new Intent("DELIVERED"), 0);
curContext.registerReceiver(
 new BroadcastReceiver()
 {
 @Overridepublicvoid onReceive(Context arg0,Intent arg1)
 {
 switch (getResultCode())
 {
 case Activity.RESULT_OK:
 Toast.makeText(getBaseContext(), "Delivered.",
 Toast.LENGTH_LONG).show();
 break;
 case Activity.RESULT_CANCELED:
 Toast.makeText(getBaseContext(), "Not Delivered: Canceled.",
 Toast.LENGTH_LONG).show();
 break;
 }
 }
 }, new IntentFilter("DELIVERED"));/* --------------------------------------------------------------- */sms.sendTextMessage(strPhone, null, strMessage,
 sentPending, deliveredPending);

对于 sendMultipartTextMessage():

String strPhone = "XXXXXXXXXXX";String strMessage = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890TEST";
SmsManager sms = SmsManager.getDefault();
ArrayList<String> messageParts = sms.divideMessage(strMessage);/* ---- Preparing Intents To Check While Sms Sent & Delivered ---- */Context curContext = this.getApplicationContext();int partsCount = messageParts.size();
ArrayList<PendingIntent> sentPendings = new ArrayList<PendingIntent>(partsCount);
ArrayList<PendingIntent> deliveredPendings = new ArrayList<PendingIntent>(partsCount);for (int i = 0; i <partsCount; i++) {
 /* Adding Sent PendingIntent For Message Part */ PendingIntent sentPending = PendingIntent.getBroadcast(curContext,
 0, new Intent("SENT"), 0);
 curContext.registerReceiver(new BroadcastReceiver() {
 @Overridepublicvoid onReceive(Context arg0, Intent arg1) {
 switch (getResultCode()) {
 case Activity.RESULT_OK:
 Toast.makeText(getBaseContext(), "Sent.",
 Toast.LENGTH_LONG).show();
 break;
 case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
 Toast.makeText(getBaseContext(),
 "Not Sent: Generic failure.", Toast.LENGTH_LONG)
. show();
 break;
 case SmsManager.RESULT_ERROR_NO_SERVICE:
 Toast.makeText(
 getBaseContext(),
 "Not Sent: No service (possibly, no SIM-card).",
 Toast.LENGTH_LONG).show();
 break;
 case SmsManager.RESULT_ERROR_NULL_PDU:
 Toast.makeText(getBaseContext(), "Not Sent: Null PDU.",
 Toast.LENGTH_LONG).show();
 break;
 case SmsManager.RESULT_ERROR_RADIO_OFF:
 Toast.makeText(
 getBaseContext(),
 "Not Sent: Radio off (possibly, Airplane mode enabled in Settings).",
 Toast.LENGTH_LONG).show();
 break;
 }
 }
 }, new IntentFilter("SENT"));
 sentPendings.add(sentPending);
 /* Adding Delivered PendingIntent For Message Part */ PendingIntent deliveredPending = PendingIntent.getBroadcast(
 curContext, 0, new Intent("DELIVERED"), 0);
 curContext.registerReceiver(new BroadcastReceiver() {
 @Overridepublicvoid onReceive(Context arg0, Intent arg1) {
 switch (getResultCode()) {
 case Activity.RESULT_OK:
 Toast.makeText(getBaseContext(), "Delivered.",
 Toast.LENGTH_LONG).show();
 break;
 case Activity.RESULT_CANCELED:
 Toast.makeText(getBaseContext(),
 "Not Delivered: Canceled.", Toast.LENGTH_LONG)
. show();
 break;
 }
 }
 }, new IntentFilter("DELIVERED"));
 deliveredPendings.add(deliveredPending);
}/* --------------------------------------------------------------- */sms.sendMultipartTextMessage(strPhone, null, messageParts,
 sentPendings, deliveredPendings);

检查设备是否可以发送短信

PackageManager pm = this.getPackageManager();if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY) &&
!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY_CDMA)) {
 Toast.makeText(this, "Sorry, your device probably can't send SMS...", Toast.LENGTH_SHORT).show();
 }

发送 MMS

彩信是带有附加文件的短信,也就是说,图像、视频、音频等。

然而,如果没有用户交互(例如),就没有简单的简单API来发送彩信。 SmsManager.sendTextMessage())目前稳定的Android版本(低于 Android 5.0 - API级别 21 )。 可以有一个"脏"低级技巧,换句话说,这里 这里 还有这里;你可以发现 com.android.mms应用程序的来源(提示, 从旧版本到新版本更简单一些)。

现在我在这里实现 ! 如果感兴趣,请检查更新。

在完成这项工作之前,我们可以通过intents发送彩信。 但是,它看起来比SMSes更依赖设备和不稳定,我们不能确定100%以下代码可以在任何设备上运行 。

带有Intent(通过短信应用)的

要求android.permission.SEND_SMS权限(请参见上方)。

/* Phone Number is just all digits of the number (including country code)
 *"+" isn't needed, but can appear
 * For example:
 * I have Beeline Phone Number +7 (962) 000-00-00,
 * in my case, it should be 79620000000 or +79620000000 */strPhone = "XXXXXXXXXXX";
strMessage = "LoremnIpsum";/* Attach Url is local (!) URL to file which should be sent */strAttachUrl = "file:///sdcard/Test.png";/* Attach Type is a content type of file which should be sent */strAttachType = "image/png";
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
sendIntent.putExtra("address", strPhone);
sendIntent.putExtra("sms_body", strMessage);/* Adding The Attach */sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(strAttachUrl));
sendIntent.setType(strAttachType);
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(sendIntent);

没有Intent(以编程方式以编程方式)

要以编程方式发送彩信,我们需要以下权限,

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"/>

大致来说,彩信是通过网络对MMSC提供者的HTTP请求。

这意味着我们首先应该获取MMSC中心的网址。 要执行此操作,我们将使用如下方法,

final Cursor apnCursor = this.getContentResolver().query(Uri.withAppendedPath(Telephony.Carriers.CONTENT_URI, "current"), null, null, null, null);if (apnCursor!= null && apnCursor.moveToFirst()) {
 do {
 finalString type = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.TYPE));
 if (!TextUtils.isEmpty(type) && (type.equalsIgnoreCase(Phone.APN_TYPE_ALL) || type.equalsIgnoreCase(Phone.APN_TYPE_MMS))) {
 String mmsc = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSC));
 String mmsProxy = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSPROXY));
 String port = apnCursor.getString(apnCursor.getColumnIndex(Telephony.Carriers.MMSPORT)); 
 //TODO }
 } while (apnCursor.moveToNext()); 
 apnCursor.close();
 }

接下来,我们将结合需要来自消息PDUs的HTTP请求,并且使用这个类发送它,

未完待续。

收到短信

1.让我们将此权限添加到 AndroidManifest.xml

<manifest...><uses-sdk.../><uses-permissionandroid:name="android.permission.RECEIVE_SMS"/><application...>... </manifest>

2.让我们基于内部BroadcastReceiver类创建一个SmsReceiver类,

publicclass SmsReceiver extends BroadcastReceiver {
 @Overridepublicvoid onReceive(Context context, Intent intent) {
 Bundle intentExtras = intent.getExtras();
 if (intentExtras!= null) {
 /* Get Messages */ Object[] sms = (Object[]) intentExtras.get("pdus");
 for (int i = 0; i <sms.length; ++i) {
 /* Parse Each Message */ SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) sms[i]);
 String phone = smsMessage.getOriginatingAddress();
 String message = smsMessage.getMessageBody().toString();
 Toast.makeText(context, phone + ":" + message, Toast.LENGTH_SHORT).show();
 }
 }
 }
}

3 。让我们在AndroidManifest.xml(in"应用程序"节)中声明我们的SmsReceiver

<manifest...>... 
 <application...>... 
 <receiverandroid:name=".SmsReceiver"android:exported="true"><intent-filterandroid:priority="2147483647"><actionandroid:name="android.provider.Telephony.SMS_RECEIVED"/></intent-filter></receiver></application></manifest>

android:name是接收类的路径,它可以是绝对(例如 。"com.example.app.SmsReceiver")或关系到主软件包(例如 。" smsreceiver"等于"your.package.name.SmsReceiver"在我们的例子中)。

正在阅读SMS历史记录

权限
<manifest...><uses-sdk.../><uses-permissionandroid:name="android.permission.READ_SMS"/><application...>... </manifest>
Short Code Piece : all
Cursor cur = getContentResolver().query(Uri.parse("content://sms/"), null, null, null, null);if (cur.moveToFirst()) { /* false = no sms */do {
 String msgInfo = "";
 for (int i = 0; i <cur.getColumnCount(); i++) {
 msgInfo += "" + cur.getColumnName(i) + ":" + cur.getString(i);
 }
 Toast.makeText(this, msgInfo, Toast.LENGTH_SHORT).show();
 } while (cur.moveToNext());
}
短代码:收件箱

content://sms/替换为 content://sms/inbox

短代码Fragment:发送

content://sms/替换为 content://sms/sent

短代码:草稿

content://sms/替换为 content://sms/draft

大代码块(你可以从上面的附件下载这个项目) AndroidManifest.xml,

应添加权限,请参见上面的。

activity_main.xml
 
  
 < 
  
LinearLayout  
  
  
  
 xmlns:android  
  
="  
  
http://schemas.android.com/apk/res/android"  
  
  
  
  
  
  
  
  
  
  
  
  
  
 xmlns:tools  
  
="  
  
http://schemas.android.com/tools"  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_width  
  
="  
  
match_parent"  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_height  
  
="  
  
match_parent"  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:orientation  
  
="  
  
vertical"  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:padding  
  
="  
  
3dp"  
  
  
  
  
  
  
  
  
  
  
  
  
  
 tools:context  
  
="  
  
${relativePackage}.${activityClass}"  
  
  
  
>  
  
 < 
  
LinearLayout  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_width  
  
="  
  
fill_parent"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_height  
  
="  
  
wrap_content"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_alignParentLeft  
  
="  
  
true"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_alignParentTop  
  
="  
  
true"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:orientation  
  
="  
  
horizontal"  
  
  
  
>  
  
 < 
  
Button  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:id  
  
="  
  
@+id/btnAll"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_width  
  
="  
  
wrap_content"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_height  
  
="  
  
wrap_content"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:text  
  
="  
  
All"  
  
  
  
/ 
  
>  
  
 < 
  
Button  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:id  
  
="  
  
@+id/btnInbox"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_width  
  
="  
  
wrap_content"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_height  
  
="  
  
wrap_content"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_marginLeft  
  
="  
  
3dp"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:text  
  
="  
  
Inbox"  
  
  
  
/ 
  
>  
  
 < 
  
Button  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:id  
  
="  
  
@+id/btnSent"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_width  
  
="  
  
wrap_content"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_height  
  
="  
  
wrap_content"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_marginLeft  
  
="  
  
3dp"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:text  
  
="  
  
Sent"  
  
  
  
/ 
  
>  
  
 < 
  
Button  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:id  
  
="  
  
@+id/btnDraft"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_width  
  
="  
  
wrap_content"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_height  
  
="  
  
wrap_content"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_marginLeft  
  
="  
  
3dp"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:text  
  
="  
  
Draft"  
  
  
  
/ 
  
>  
  
 < 
  
/LinearLayout  
  
>  
  
 < 
  
ScrollView  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_width  
  
="  
  
fill_parent"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_height  
  
="  
  
fill_parent"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_marginTop  
  
="  
  
3dp"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:scrollbars  
  
="  
  
vertical"  
  
  
  
>  
  
 < 
  
HorizontalScrollView  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_width  
  
="  
  
fill_parent"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_height  
  
="  
  
fill_parent"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:scrollbars  
  
="  
  
horizontal"  
  
  
  
>  
  
 < 
  
TableLayout  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:id  
  
="  
  
@+id/tblMain"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_width  
  
="  
  
fill_parent"  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
 android:layout_height  
  
="  
  
fill_parent"  
  
>  
  
 < 
  
/TableLayout  
  
>  
  
 < 
  
/HorizontalScrollView  
  
>  
  
 < 
  
/ScrollView  
  
>  
  
 < 
  
/LinearLayout  
  
>  
 
MainActivity.java
 
  
package  
com.example.androidreadsms ;  
import  
android.app. Activity ;  
import  
android.database.Cursor ;  
import  
android.net. Uri ;  
import  
android.os. Bundle ;  
import  
android.view. Gravity ;  
import  
android.view. Menu ;  
import  
android.view. MenuItem ;  
import  
android.view. View ;  
import  
android.view. View.OnClickListener ;  
import  
android.widget.Button ;  
import  
android.widget.TableLayout ;  
import  
android.widget.TableRow ;  
import  
android.widget.TextView ;  
import  
android.widget.Toast ;  
public  
  
class  
MainActivity  
extends  
Activity  
implements  
OnClickListener { Button btnAll; Button btnInbox; Button btnSent; Button btnDraft; TableLayout tblMain ;  
@Override  
  
protected  
  
void  
onCreate(Bundle savedInstanceState) {  
super  
.onCreate(savedInstanceState); setContentView(R.layout.activity_main) ;  
try  
{  
/*  
  
* Initializing Widgets */ 
btnAll = (Button) findViewById(R.id.btnAll); btnAll.setOnClickListener (  
this  
); btnInbox = (Button) findViewById(R.id.btnInbox); btnInbox.setOnClickListener (  
this  
); btnSent = (Button) findViewById(R.id.btnSent); btnSent.setOnClickListener (  
this  
); btnDraft = (Button) findViewById(R.id.btnDraft); btnDraft.setOnClickListener (  
this  
); tblMain = (TableLayout) findViewById(R.id.tblMain); }  
catch  
(Exception ex) { Toast.makeText (  
this  
,  
"  
  
Error in MainActivity.onCreate :"  
+ ex.getMessage(), Toast.LENGTH_SHORT).show(); } }  
@Override  
  
public  
  
void  
onClick(View v) { Uri smsUri = Uri.parse (  
"  
  
content://sms/"  
) ;  
switch  
(v.getId()) {  
case  
R.id. btnInbox : smsUri = Uri.parse (  
"  
  
content://sms/inbox"  
) ;  
break  
;  
case  
R.id. btnSent : smsUri = Uri.parse (  
"  
  
content://sms/sent"  
) ;  
break  
;  
case  
R.id. btnDraft : smsUri = Uri.parse (  
"  
  
content://sms/draft"  
) ;  
break  
; } Cursor cursor = getContentResolver().query(smsUri, null, null, null, null); Cursor2TableLayout(cursor, tblMain); }  
public  
  
void  
Cursor2TableLayout(Cursor cur, TableLayout tblLayout) {  
/*  
  
Clearing Table if contains any Rows/Headers */ 
tblLayout.removeAllViews() ;  
/*  
  
Moving to first */ 
  
if  
(!cur.moveToFirst()) {  
/*  
  
false = cursor is empty */ 
  
return  
; }  
/*  
  
Column Headers */ 
TableRow headersRow =  
new  
TableRow (  
this  
) ;  
for  
(  
int  
j =  
0  
; j <cur.getColumnCount(); j++) { TextView textView =  
new  
TextView (  
this  
); textView.setGravity(Gravity.CENTER_HORIZONTAL); textView.setText(cur.getColumnName(j)); textView.setPadding (  
0  
,  
0  
,  
5  
,  
0  
) ;  
if  
(Build.VERSION.SDK_INT> =  
11  
) {  
/*  
  
if running Android 3.0 + */ 
textView.setAlpha (  
0  
.8f); }  
else  
{  
/*  
  
Phones with Android 2.x are a rarity now, * but until recently (June, 2015 ) i have seen them * in the store... * Also, i myself have such gadget and using it sometimes... */ 
AlphaAnimation animation =  
new  
AlphaAnimation (  
0  
.8f,  
0  
.8f); animation.setDuration (  
0  
); animation.setFillAfter(true); textView.startAnimation(animation); } headersRow.addView(textView); } headersRow.setPadding (  
10  
,  
10  
,  
10  
,  
10  
); tblLayout.addView(headersRow) ;  
/*  
  
Rows */ 
  
for  
(  
int  
i =  
0  
; i <cur.getCount(); i++) { TableRow tableRow =  
new  
TableRow (  
this  
) ;  
for  
(  
int  
j =  
0  
; j <cur.getColumnCount(); j++) { TextView textView =  
new  
TextView (  
this  
); textView.setGravity(Gravity.CENTER_HORIZONTAL); textView.setText(cur.getString(j)); textView.setPadding (  
0  
,  
0  
,  
5  
,  
0  
); tableRow.addView(textView); } tableRow.setPadding (  
10  
,  
10  
,  
10  
,  
10  
); tblLayout.addView(tableRow); cur.moveToNext(); } cur.close(); } } 
讨论
巾帼英雄 profile image