日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Android基礎教程之----SMS簡單發送短信程序(兩

系統 1999 0

前面的范例,示范了如何通過程序撥打電話,在 GSM 移動通信系統的服務中,除了打電話外,另一個常用的功能,就是發短信.也因為如此,許多電信業者推出專屬短信族的專用費率,由此可知短信功能對手機的重要性.

發送短信的關鍵程序是通過 SmsManager 對象的 sendTextMessage() 方法來完成,其中 sendTextMessage() 方法需傳入五個值,依次是收件人地址( String ),發送地址( String ),發送服務( PendingIntent )與送達服務( PendingIntent ),其中收件人與正文是不可為 null 的兩個參數.

本例子通過兩個模擬器, 5554 , 5556 互相通信,下面我將分5個步驟,講一下發送短信程序是如何實現的.

Step 1 :建立一個 Android 工程,我們命名為 SMSDemo .

Step 2 :設計一下程序的 UI ,也就是主界面 main.xml ,這里用 AbsoluteLayout ,有點丑見笑了!代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android=" http://schemas.android.com/apk/res/android "
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="收件 人:"
android:textSize="16sp"
android:layout_x="0px"
android:layout_y="12px"
>
</TextView>
<EditText
android:id="@+id/myEditText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="18sp"
android:layout_x="60px"
android:layout_y="2px"
>
</EditText>
<EditText
android:id="@+id/myEditText2"
android:layout_width="fill_parent"
android:layout_height="223px"
android:text=""
android:textSize="18sp"
android:layout_x="0px"
android:layout_y="52px"
>
</EditText>
<Button
android:id="@+id/myButton1"
android:layout_width="162px"
android:layout_height="wrap_content"
android:text="發送短信"
android:layout_x="80px"
android:layout_y="302px"
>
</Button>
</AbsoluteLayout>

Step 3: 主控制程序 SMSDemo.java 如下:

package com.android.test;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;

import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SMSDemo extends Activity {

private Button mButton1;
private EditText mEditText1;
private EditText mEditText2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//獲取資源
mEditText1 = (EditText)findViewById(R.id.myEditText1);
mEditText2 = (EditText)findViewById(R.id.myEditText2);
mButton1 = (Button)findViewById(R.id.myButton1);
//發送短信的響應
mButton1.setOnClickListener(new Button.OnClickListener(){

public void onClick(View v) {
//獲取發送地址和發送內容
String messageAddress = mEditText1.getText().toString();
String messageContent = mEditText2.getText().toString();

//構建一取得default instance的SmsManager對象

SmsManager smsManager = SmsManager.getDefault();
//檢查輸入內容是否為空,這里為了簡單就沒有判斷是否是號碼,短信內容長度的限制也沒有做
if(messageAddress.trim().length()!=0 && messageContent.trim().length()!=0)
{
try{
PendingIntent pintent = PendingIntent.getBroadcast(SMSDemo.this, 0, new Intent(), 0);
smsManager.sendTextMessage(messageAddress, null, messageContent, pintent, null);

}catch(Exception e)
{
e.printStackTrace();
}
//提示發送成功
Toast.makeText(SMSDemo.this, "發送成功", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(SMSDemo.this, "發送地址或者內容不能為空", Toast.LENGTH_SHORT).show();
}
}

});
}
}

Step 4: 增加撥打電話權限 AndroidManifest.xml 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=" http://schemas.android.com/apk/res/android "
package="com.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SMSDemo"
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>
<uses-sdk android:minSdkVersion="3" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
</manifest>

Step 5:run it! 效果圖如下, 5554 5556 發送了一條短信:

Android基礎教程之----SMS簡單發送短信程序(兩個模擬器之間的通信)! Android基礎教程之----SMS簡單發送短信程序(兩個模擬器之間的通信)!

Ok~今天到此結束,謝謝大家關注!

Android基礎教程之----SMS簡單發送短信程序(兩個模擬器之間的通信)!


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 汤阴县| 陇南市| 叙永县| 西畴县| 中江县| 冕宁县| 南部县| 金堂县| 广河县| 广昌县| 桂林市| 佛坪县| 广丰县| 区。| 勐海县| 华安县| 石嘴山市| 安乡县| 台东市| 加查县| 海兴县| 镶黄旗| 布拖县| 皮山县| 西贡区| 古田县| 大理市| 新巴尔虎左旗| 竹山县| 花莲市| 兰州市| 揭阳市| 白朗县| 炉霍县| 竹山县| 奈曼旗| 大化| 弥渡县| 德清县| 体育| 拉萨市|