Android SmsManager(短信管理器),发送短信息

AndroidManifest.xml

<uses-permission android:name="android.permission.SEND_SMS"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="shortcut.song.com.myapplication.SendSmsActivity">

    <EditText
        android:id="@+id/send_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/send_msg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Send"/>
LinearLayout>
package shortcut.song.com.myapplication;

import android.app.PendingIntent;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
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 SendSmsActivity extends AppCompatActivity {
    EditText number, sendmsg;
    Button btnSend;
    SmsManager smsManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_sms);
        // 获取SmsManager
        smsManager = SmsManager.getDefault();

        number = (EditText)findViewById(R.id.send_number);
        sendmsg = (EditText)findViewById(R.id.send_msg);
        btnSend = (Button)findViewById(R.id.btn_send);

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建一个PendingIntent对象
                PendingIntent pi = PendingIntent.getActivity(SendSmsActivity.this, 0, new Intent(), 0);
                // 发送信息
                smsManager.sendTextMessage(number.getText().toString(), null, sendmsg.getText().toString(), pi, null);
                // 提示发送完成
                Toast.makeText(SendSmsActivity.this, "发送完成", Toast.LENGTH_SHORT).show();
            }
        });

    }
}

运行效果:

Android SmsManager(短信管理器),发送短信息_第1张图片

你可能感兴趣的:(Android)