随笔 - 750  文章 - 1  评论 - 107  阅读 - 34万

[Android] 开发第六天

短信发送器

先了解一下 Android 的几种通知方式:

1. 通知栏通知

2. 弹窗通知

3. 吐司通知

接下来上代码,做一个可发送短信的 Android 程序:

MainActivity.java 文件:

复制代码
package oazzz.cn.test4;

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;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    private EditText numberText;
    private EditText contextText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        numberText = (EditText) this.findViewById(R.id.number);
        contextText = (EditText) this.findViewById(R.id.content);
        Button button = (Button) this.findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String number = numberText.getText().toString();
                String content = contextText.getText().toString();
                SmsManager manager = SmsManager.getDefault();
                ArrayList<String> texts = manager.divideMessage(content);
                for (String text : texts) {
                    // 接收手机号 短信中心地址,null 为默认 短信内容(有长度限制) 得到发送状况 得到对方短信回执 这两个使用的异步
                    manager.sendTextMessage(number, null, content, null, null);
                }
                Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_SHORT);
            }
        });
    }
}
复制代码
申请发短信权限 AndroidManifest.xml 文件
<...>
    <application
        ...
    </application>
    <uses-permission android:name="android.permission.SEND_SMS" />
</manifest>

布局文件 activity_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="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/number" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="phone"
        android:id="@+id/number"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/content" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minLines="3"
        android:id="@+id/content"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:id="@+id/button"/>
</LinearLayout>
复制代码

实际发布到安卓手机上时,没注意到吐司弹窗提示。

posted on   z5337  阅读(140)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示