趙雅智_Android簡訊發送器

來源:互聯網
上載者:User

標籤:android   style   class   blog   c   code   


注意要點:

1)必須要在AndroidManifest.xml中添加傳送簡訊許可權<uses-permission android:name="android.permission.SEND_SMS" />

設定視圖:setContentView(R.layout.布局xml檔案);

2)尋找控制項:findViewById(R.id.控制項id);

3)監聽按鈕事件:控制項.setOnClickListener(this),實現OnClickListener介面

4)擷取editText裡的值:getTex();

5)傳送簡訊思路:

a) 擷取簡訊號碼及內容

b) 判斷簡訊號碼及內容是否為空白

c) 擷取短息的管理器對象

d) 如果你的字元數大於了70拆分

e) 傳送簡訊的處理

 

運行效果如下:

     

              

 

 

String檔案代碼:

 

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">lesson02</string>    <string name="hello_world">Hello world!</string>    <string name="action_settings">Settings</string>    <string name="sms_tip_phone">請輸入您要發送的手機號碼</string>    <string name="sms_tip_content">請輸入您要發送的資訊內容</string>    <string name="tip_sms">輸入短息的資訊</string>    <string name="send_sms">傳送簡訊</string>    <string name="error_sms_content">您發送的簡訊號碼或者簡訊內容不可為空</string></resources>

布局檔案 activity_sms.xml:

 

<RelativeLayout    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"    tools:context="${packageName}.${activityClass}">    <!-- 提示輸入電話號碼資訊 -->    <TextView         android:id="@+id/tv_tip_phone"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/sms_tip_phone"/>        <!-- 輸入手機號碼的資訊 -->    <EditText        android:id="@+id/et_phone"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/tv_tip_phone"        android:layout_alignParentLeft="true"        android:inputType="phone"/>        <!-- 提示輸入簡訊資訊 -->    <TextView         android:id="@+id/tv_tip_sms_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="@string/sms_tip_content"        android:layout_below="@+id/et_phone"/>        <!-- 輸入短息的資訊 -->    <EditText        android:id="@+id/et_sms_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/tv_tip_sms_content"        android:lines="5"        android:hint="@string/tip_sms"/>       <!-- 發送資訊的按鈕 -->    <Button         android:id="@+id/send_btn"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_below="@+id/et_sms_content"        android:text="@string/send_sms"/></RelativeLayout>

Activity代碼:SmsActivity.java

 

package com.example.lesson02_sms;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.telephony.SmsManager;import android.text.TextUtils;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class SmsActivity extends Activity implements OnClickListener{ //聲明控制項private EditText et_phone;private EditText et_content;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);//設定顯示的視圖setContentView(R.layout.activity_sms);//擷取控制項對象et_phone = (EditText) findViewById(R.id.et_phone);et_content = (EditText) findViewById(R.id.et_sms_content);//擷取按鈕的控制項對象Button send_btn = (Button) findViewById(R.id.send_btn);//註冊監聽事件send_btn.setOnClickListener(this);}/** * 按鈕點擊事件處理的方法 */@Overridepublic void onClick(View v) {//擷取控制項的id值 int id =  v.getId(); //根據id判斷點擊的控制項 switch (id) {case R.id.send_btn://擷取簡訊號碼 String phone =  et_phone.getText().toString(); //擷取簡訊的內容 String content = et_content.getText().toString(); //判斷簡訊號碼和內容是否為空白 if(TextUtils.isEmpty(phone)||TextUtils.isEmpty(content)){ //多士效果 Toast.makeText(this,R.string.error_sms_content, Toast.LENGTH_SHORT).show(); }else{ //擷取短息的管理器對象 SmsManager smsManager = SmsManager.getDefault();  //如果你的字元數大於了70就開始拆了. ArrayList<String> smscontents =  smsManager.divideMessage(content);  //遍曆發送資訊 for(int i=0;i<smscontents.size();i++){ //傳送簡訊的處理 smsManager.sendTextMessage(phone, null, smscontents.get(i), null, null); } //多士效果 Toast.makeText(this, "發送資訊成功", Toast.LENGTH_LONG).show(); }break;default:break;}}}

 

設定檔AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.lesson02_sms"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <!-- 傳送簡訊的許可權 -->    <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="com.example.lesson02_sms.SmsActivity"            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></manifest>


 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.