Android中實現簡訊發送的一種方式

來源:互聯網
上載者:User

標籤:center   png   實現   ali   action   launch   err   into   bubuko   

SendSmsActivity.java:

package com.test.smsmangerdemo.sendsmsactivity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.content.Intent;import android.app.PendingIntent;import android.telephony.SmsManager;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;/** *傳送簡訊執行個體 */public class SendSmsActivity extends AppCompatActivity {    EditText phone, content;    Button send;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_send_sms);        //擷取 SMSManger管理器        final SmsManager smsManager = SmsManager.getDefault();        //初始化控制項        phone = (EditText) findViewById(R.id.et_phone);        content = (EditText) findViewById(R.id.et_content);        send = (Button) findViewById(R.id.btn_send);        send.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                //建立一個 android.app.PendingIntent 對象                PendingIntent pi = PendingIntent.getActivity(SendSmsActivity.this, 0, new Intent(), 0);                //傳送簡訊                smsManager.sendTextMessage(phone.getText().toString(), null, content.getText().toString(),                        pi, null);                //提示簡訊發送完成                Toast.makeText(SendSmsActivity.this, "簡訊發送完成", Toast.LENGTH_SHORT).show();            }        });    }}

AndroidMainfest.xml:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.test.smsmangerdemo.sendsmsactivity" >    <uses-permission android:name="android.permission.SEND_SMS"/>    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme" >        <activity android:name=".SendSmsActivity" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

activity_send_sms.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns: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"tools:context="com.test.smsmangerdemo.sendsmsactivity.SendSmsActivity"><LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="horizontal">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="收件者"/>    <EditText        android:id="@+id/et_phone"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"/></LinearLayout><LinearLayout    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:layout_marginTop="10dp"    android:orientation="horizontal">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="發送內容"/>    <EditText        android:id="@+id/et_content"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="10dp"        android:gravity="top"        android:lines="5"        android:text="你好"/></LinearLayout><Button    android:layout_gravity="center_horizontal"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="發送"    android:id="@+id/btn_send"    /></LinearLayout>

 實現效果:

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.