您的位置:首页 > 教育 > 培训 > Android 发送短信功能

Android 发送短信功能

2024/10/6 10:42:13 来源:https://blog.csdn.net/weixin_46107120/article/details/140907730  浏览:    关键词:Android 发送短信功能

Android 发送短信功能

Android 发送短信功能
输入短信内容和手机号码后,跳转到系统短信功能,然后点击发送即可

MainActivity.Java

import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;public class MainActivity extends AppCompatActivity {private EditText smsNumber;private EditText smsContent;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initView();}private void initView() {smsContent = (EditText) findViewById(R.id.sms_content);smsNumber = (EditText) findViewById(R.id.sms_number);Button smsButton = (Button) findViewById(R.id.sms_button);smsButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {sendSmsByIntent();}});}private void sendSmsByIntent() {Intent intent = new Intent(Intent.ACTION_SENDTO);intent.putExtra("sms_body", smsContent.getText().toString());Uri data = Uri.parse("smsto:" + smsNumber.getText().toString());intent.setData(data);startActivity(intent);}
}

activity_main.xml

<?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=".MainActivity"><EditTextandroid:id="@+id/sms_content"android:layout_width="match_parent"android:layout_height="200dp"android:layout_marginTop="20dp"android:gravity="top|left"android:hint="输入发送的内容"android:singleLine="false"android:textSize="22sp" /><EditTextandroid:id="@+id/sms_number"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:hint="输入号码"android:inputType="phone"android:singleLine="true"android:textSize="22sp" /><Buttonandroid:id="@+id/sms_button"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:text="发送" /></LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"><applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.Learn"><activityandroid:name=".MainActivity"android:exported="true"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com