Android -- I/O CALL SMS Layout
Layout--布局
常用的的就是線性布局:
<!--這是個線性布局-->
<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屬性
以 @+id 開頭,那么R.java資源文件就會自動把它加入到id子類中,框架嘛,顯示在屏幕上的時候就已經創建了,你獲取不獲取,它都在哪里,一個實例,在代碼中這樣獲取
他返回的是個View,需要自己強轉成期望的類型。
Intent攜帶數據(打電話)轉向CALL(打電話) 以及 短信
這些操作需要權限,要更改 AndroidManifest.xml 文件
添加一下標簽,與<application>標簽同級別
<uses-permission android:name="android.permission.CALL_PHONE"/>
<!-- 申請用戶授權 短信發送-->
<uses-permission android:name="android.permission.SEND_SMS"/>
---------------------------------------------------------
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/文件
浙公網安備 33010602011771號