本文主要是介绍Android SmsManager(短信管理器),发送短信息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
AndroidManifest.xml
<uses-permission android:name="android.permission.SEND_SMS"/>
<?xml version="1.0" encoding="utf-8"?>
<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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_send_sms);// 获取SmsManagersmsManager = 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() {@Overridepublic 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(短信管理器),发送短信息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!