android 之手机拨号器,以及短信发送器的简单实现
android手机拨号器,以及短信发送器的简单实现
其中本例中的手机拨号器并不是我们自己完全去实现,而是通过intent让android系统自带的拨号器去实现拨号的功能,
一、电话拨号器
第一步:对资源文件夹values中的string.xml文件进行配置,(在应用程序中需用到的字符串资源如button按钮上的名字、TextView中的内容都在此定义,然后再布局文件中可以进行使用)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, PhoneActivity!</string>
<string name="app_name">电话拨号器</string>
<string name="textview_name">请输入电话号码</string>
<string name="button_name">拨号</string>
</resources>
第二步:对资源中布局文件夹下的main.xml文件进行配置,该文件控制Activity的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/textview_name"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/edittext_id"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_name"
android:id="@+id/button_id"
/>
</LinearLayout>
第三步:实现包含业务逻辑的具体的Activity
package com.heima.phone;
import android.app.Activity;
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 PhoneActivity extends Activity {
EditText editText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText = (EditText) this.findViewById(R.id.edittext_id);
Button button = (Button) this.findViewById(R.id.button_id);
button.setOnClickListener(new PhoneListener());
}
private final class PhoneListener implements View.OnClickListener{
/*
*<action android:name="android.intent.action.CALL" />
*<category android:name="android.intent.category.DEFAULT" />
*<data android:scheme="tel" />
*/
@Override
public void onClick(View v) {
String telephone = editText.getText().toString();
Intent intent = new Intent();
intent.setAction("android.intent.action.CALL");
intent.setData(Uri.parse("tel:"+telephone));
startActivity(intent);// 隐式注册android.intent.category.DEFAULT,因此无需显示调用intent.setAction("android.intent.action.CALL");
}
}
}
二、短信发送器
第一步:配置资源文件string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, SMSActivity!</string>
<string name="app_name">短信发送器</string>
<string name="telephone_number">请输入手机号码</string>
<string name="sms_content">请输入短信内容</string>
<string name="buttonname">发送短信</string>
<string name="show">发送成功</string>
</resources>
第二步:配置布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/telephone_number"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/phone_id"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/sms_content"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="3"
android:id="@+id/sms_id"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button_id"
/>
</LinearLayout>
第三步:实现Activity
package com.heima.sms;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SMSActivity extends Activity {
EditText phone;
EditText sms;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
phone = (EditText) this.findViewById(R.id.phone_id);
sms = (EditText) this.findViewById(R.id.sms_id);
Button button = (Button) this.findViewById(R.id.button_id);
button.setOnClickListener(new SMSListener());
}
private final class SMSListener implements View.OnClickListener{
@Override
public void onClick(View v) {
String phoneNumber = phone.getText().toString();
String smscontent = sms.getText().toString();
//得到短信管理器
SmsManager manager = SmsManager.getDefault();
//divideMessage方法用于分割内容过长的短信
ArrayList<String> contents = manager.divideMessage(smscontent);
for(String content:contents){
/* 各个参数的含义
* destinationAddress 短信接收者号码、
* scAddress 短信转发中心号码,一般为默认为null即可
* text 短信内容sentIntent 不太清楚,暂时设为null
* deliveryIntent 不太清楚,暂时设为null
*/
manager.sendTextMessage(phoneNumber, null, content, null, null);
}
//不要忘记写show方法,否则没有信息发送状态提示,Toast.LENGTH_LONG标示显示时间稍长
Toast.makeText(getApplicationContext(), R.string.show, Toast.LENGTH_LONG).show();
}
}
}
特别注意在短信发送器的例子中一定要注意在AndroidManifest.xml文件中配置属性,
<uses-permission android:name="android.permission.SEND_SMS"/>
否则老是报程序异常终止异常
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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 让容器管理更轻松!
2008-07-04 javascrip 常用属性