簡訊發送器(1.0版)

來源:互聯網
上載者:User

標籤:

這一節學習了簡訊發送功能的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();                              }      }}

 

簡訊發送器(1.0版)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.