标签:
这一节学习了短信发送功能的app开发,效果如图:
前台,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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.swust.msg.MainActivity" > <!-- 输入为phone类型:0~9和#* --> <EditText android:id="@+id/et_phone" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/tishiPhone" android:inputType="phone" /> <!-- 输入限制为五行,输入框中的内容顶部对齐 --> <EditText android:id="@+id/et_msg" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="top" android:hint="@string/tishiMsg" android:lines="5" /> <Button android:id="@+id/btnSend" android:layout_width="119dp" android:layout_height="64dp" android:text="发送" android:onClick="sendMsg"/> </LinearLayout>
后台,MainActivity.java代码:
package com.swust.msg; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sendMsg(View v){ //获取到用户输入的号码和内容 EditText et_phone = (EditText)findViewById(R.id.et_phone); EditText et_msg = (EditText)findViewById(R.id.et_msg); String phone=et_phone.getText().toString(); String msg=et_msg.getText().toString(); //发送短信 //1.获取短息管理器 SmsManager sm = SmsManager.getDefault(); //2.发送短信 /*第二个形参为短息服务中心,为空即可 *第三个形参为发送广播,为空即可 *第4个形参为接受广播,为空即可*/ //sm.sendTextMessage(phone, null, msg, null, null); //2.切割短信,把长短信分成若干个小短信 ArrayList<String> smss= sm.divideMessage(msg); //3.for循环把集合中所有短信全部发出去 for(String string:smss){ sm.sendTextMessage(phone, null, string, null, null); } } }
当然需要加权限,在清单文件Permission中有如下权限:
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.SEND_SMS"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
第二版:
关于发送短信的核心语句解释参照这里:
http://www.cnblogs.com/nktblog/articles/2607086.html
package com.swust.msg; import java.util.ArrayList; import android.app.Activity; import android.app.PendingIntent; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsManager; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void sendMsg(View v){ //获取到用户输入的号码和内容 EditText et_phone = (EditText)findViewById(R.id.et_phone); EditText et_msg = (EditText)findViewById(R.id.et_msg); String phone=et_phone.getText().toString(); String msg=et_msg.getText().toString(); //发送短信 //1.获取短息管理器 SmsManager sm = SmsManager.getDefault(); //2.发送短信 /*第二个形参为短息服务中心,为空即可 *第三个形参为发送广播,为空即可 *第4个形参为接受广播,为空即可*/ //sm.sendTextMessage(phone, null, msg, null, null); //2.切割短信,把长短信分成若干个小短信 ArrayList<String> smss= sm.divideMessage(msg); //3.for循环把集合中所有短信全部发出去 for(String string:smss){ sm.sendTextMessage(phone, null, string, null, null); } /*首先判读电话长度为11或者6(测试我们学校)并且内容不为空,将信息发送出去;发送成功或失败后返回提示 缺点是,没有使用切割短信循环发功能,直接将信息(msg)发送了*/ if( (phone.trim().length()==11||phone.trim().length()==6) &&msg.trim().length()!=0){ PendingIntent pintent=PendingIntent.getBroadcast(MainActivity.this, 0, new Intent(), 0); sm.sendTextMessage(phone, null, msg, pintent, null); Toast.makeText(MainActivity.this, "发送成功", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this, "发送失败", Toast.LENGTH_SHORT).show(); } } }
标签:
原文地址:http://www.cnblogs.com/shuqingstudy/p/4874923.html