Android -- I/O CALL SMS Layout

Layout--布局

常用的的就是线性布局:

复制代码
<?xml version="1.0" encoding="utf-8"?>

<!--这是个线性布局-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<!--这个属性规定它是纵向方式排列控件--
>
android:orientation="vertical"
<!--前面加了layout_的属性,就是相对于父控件的属性,这里是填充父控件-->
android:layout_width="fill_parent"
<!--填充父控件-->
android:layout_height="fill_parent"
<!--长宽都填充了父控件,它是最顶层的,于是撑满了整个屏幕-->

>

<!--我想让两个控件平行排布,但是父控件是纵向排布,那就再嵌套一个纵向排列的线性布局-->
<LinearLayout android:orientation="horizontal"
<!-- 和父类的一个意思 --
>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>

<!-- 一个文本框 -->
<TextView

<!--这个属性,是宽度按照内容的宽度来定-->
android:layout_width="wrap_content"

android:layout_height="fill_parent"

<!-- 文本是通过资源获取的 -->
android:text="@string/tvPhoneNum"
/>

<!--一个输入框-->
<EditText android:id="@+id/etPhoneNum" <!-- id,定义它,就用这种格式 @+id/ -->
android:layout_width="fill_parent"
android:layout_height="wrap_content"

/>

</LinearLayout>

<TextView
android:layout_width="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/tvMessage"
/>

<EditText
android:id="@+id/etMessage"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
android:layout_weight
="1"
android:gravity
="top|left"


/>

<LinearLayout
android:orientation="horizontal"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
<!-- 这个属性规定其内容靠右 --
>
android:gravity="right"
>
<Button
android:id="@+id/btnHistroy"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
<!-- 控件的比重,当它为1,其它控件没有设置的时候,它优先填充剩余的部分,
多于一个控件时,根据设置了这个属性的控件的个数,然后与每个控件中这个属性得值运算的出每个控件的填充比例
--
>
android:layout_weight="1"
android:text="@string/btnHistroy"
/>
<Button
android:id="@+id/btnCall"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="@string/btnCall"
/>

<Button
android:id="@+id/btnSendMessage"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:text
="@string/btnSendMessage"

/>


</LinearLayout>

</LinearLayout>
复制代码

标签中的属性,也就两种,一种是规定自己相对于自身内容的外观,一种是相对于父控件的外观(以layout_开头),也就是这个控件的超系统子系统(自己是首先想到了TRIZ 才这样理解起来)。

编码--获取layout中的控件对象

在layout中为某个控件定义了id属性

android:id="@+id/idName"

@+id 开头,那么R.java资源文件就会自动把它加入到id子类中,框架嘛,显示在屏幕上的时候就已经创建了,你获取不获取,它都在哪里,一个实例,在代码中这样获取

findViewById(R.id.idName)

他返回的是个View,需要自己强转成期望的类型。

Intent携带数据(打电话)转向CALL(打电话) 以及 短信

这些操作需要权限,要更改 AndroidManifest.xml 文件

添加一下标签,与<application>标签同级别

<!-- 申请用户授权 电话拨打-->
<uses-permission android:name="android.permission.CALL_PHONE"/>

<!-- 申请用户授权 短信发送-->
<uses-permission android:name="android.permission.SEND_SMS"/>


---------------------------------------------------------



复制代码
//获取Intent
Intent i=new Intent();

//设置要转向的Activity
i.setAction(Intent.ACTION_CALL);

//设置参数
i.setData(Uri.parse("tel:"+num));

//开始转向
startActivity(i);
复制代码

Intent  可以理解为意图,它是联系每个活动(视图活动、Activity)的纽带

Intent.ACTION_CALL 就是电话那个视图。。。。。

把电话号码穿过去,电话号码是个URI,格式是 "tel:电话号码"

复制代码
//短信管理器
SmsManager smMessage=SmsManager.getDefault();

//从控件获得短信内容
String message=etMessage.getText().toString();


if (message !=null && !"".equals(message)){

//都知道每条短信有字数限制,这个方法就是自动帮你拆分短信
ArrayList<String> messages=smMessage.divideMessage(message);


for(String sms:messages){

//发送拆分后的短信
smMessage.sendTextMessage(num, null, sms, null, null);


}

}
复制代码

sendTextMessage有很多参数

void android.telephony.SmsManager.sendTextMessage(String destinationAddress, String scAddress, String text, PendingIntent sentIntent, PendingIntent deliveryIntent)

看看手册,第一个目标地址,第二个源地址,空的话就是本机的号,第三个短信内容,后两个都可以为空

I/O操作文件

linux权限很高,每个程序都有自己的uid,dalvik又是每个程序一个虚拟机,uid根据包名来判断

//获得一个某文件的输出流
FileOutputStream fio=openFileOutput("myHistroy.txt", MODE_APPEND| MODE_PRIVATE);

//输入流
openFileOutput("myHistroy.txt");

既然每个程序的uid都不同,那么每个程序就相当于一个独立的用户了,它们都有自己的用户目录,存储自己的数据,位于
/data/data/程序包名

按着上面的方法创建的文件,属于这个文件夹下面得东西

/data/data/程序包名/files/文件

posted on   黑暗伯爵  阅读(600)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述

导航

< 2011年7月 >
26 27 28 29 30 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 6

统计

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