Android簡訊發送器實現方法_Android

來源:互聯網
上載者:User

本文執行個體講述了Android簡訊發送器實現方法。分享給大家供大家參考。具體如下:

這裡類比android簡訊發送器的實現

AndroidManifest.xml資訊清單檔:

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

main.xml布局檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"  android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="115dip"  android:layout_height="wrap_content"   android:text="請輸入手機號"  android:id="@+id/mobilelabel" /> <EditText android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:layout_toRightOf="@id/mobilelabel"   android:text="5556"  android:id="@+id/mobile" /> </RelativeLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content"  android:text="請輸入簡訊內容" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content"  android:minLines="3" android:text="I am a teacher!" android:id="@+id/content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content"  android:text="發送" android:id="@+id/button" /></LinearLayout>

MainActivity類:

package com.ljq.sms;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity { private EditText mobileText=null; private EditText contentText=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mobileText=(EditText)findViewById(R.id.mobile); contentText=(EditText)findViewById(R.id.content); Button button=(Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener(){  public void onClick(View v) {  String mobile=mobileText.getText().toString();  String content=contentText.getText().toString();  //取得android系統中預設的簡訊管理器  SmsManager manager=SmsManager.getDefault();  //如果簡訊內容過長時,則對簡訊內容進行拆分  ArrayList<String> texts=manager.divideMessage(content);  for(String text:texts){   //第一個參數:對方手機號碼   //第二個參數:簡訊中心號碼,一般設定為空白   //第三個參數:簡訊內容   //第四個參數:sentIntent判斷簡訊是否發送成功,如果你沒有SIM卡,或者網路中斷,則可以通過這個intent來判斷。   //注意強調的是“發送”的動作是否成功。那麼至於對於對方是否收到,另當別論   //第五個參數:當簡訊發送到收件者時,會收到這個deliveryIntent。即強調了“發送”後的結果   //就是說是在"簡訊發送成功"和"對方收到此簡訊"才會啟用sentIntent和deliveryIntent這兩個Intent。這也相當於是順延強制了Intent   manager.sendTextMessage(mobile, null, text, null, null);  }  //Toast.makeText(getApplicationContext(), "發送成功", Toast.LENGTH_LONG).show();  Toast.makeText(MainActivity.this, "發送成功", Toast.LENGTH_LONG).show();  } }); }}

運行結果:

希望本文所述對大家的Android程式設計有所協助。

聯繫我們

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