android實習程序 ——短信
短信格式:
電話號碼:(3/4位)區號 + (7/8)號碼
中國電話號碼難以處理
短信的uri以content://sms開頭
短信
body //內容
type //類型(別人發給我或發給別人)1:接受,2:發出(除1以外的)
date //時間
id: //可能不準確
Thread_id//準確,短信會話的分組依據
read() //0表示未讀,1表示已讀
address //地址:電話號碼
O2O :線上to線下
中國工商銀行 2303億
添加滾動條
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
添加在最外層
滾動條的高度
短信顯示的高度
android的屏幕也是有座標的
以左上角爲原點,向右爲X軸的正方向,向下爲Y軸的正方向
滾動條的高度最多隻能是屏幕的高度,但是短信顯示的高度可以超出屏幕
主線程也可稱爲UI線程,與界面的響應度有關
優先度從大到小的排列的線程
Activity運行在主線程中,創建也是這裏面
執行oncreate()、onstart()、onresume()
查詢看看用戶是否有ontouch()事件:觸摸事件也爲主線程
執行handler、service
主線程忙,須有優先度的排列
Ctrl + Shift + F :java 中的格式化
注意把搜狗輸入法關掉,搜狗輸入法會佔用該快捷鍵
阿里雲 的 android SDK
1、短信界面
2、在Log中查看短信
3、加滾動條的從下往上顯示短信
===========================================
1、短信界面
sms_detail_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom"
android:background="#f0f0fd">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/cvsn_bg_left"
android:text="hellosfsdf"
android:textColor="#0d0d0d"
android:gravity="right"
android:layout_gravity="left"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/cvsn_bg_right"
android:text="hi"
android:textColor="#0d0d0d"
android:gravity="right"
android:layout_gravity="right"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="48dp"
android:layout_marginTop="4dp"
android:layout_weight="1.25"
android:background="@drawable/cvsn_bg_right"
android:gravity="right"
android:text="Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2"
android:textColor="#0d0d0d" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2013/07/10 11:35"
android:textColor="#0d0d0d"
android:paddingRight="16dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginTop="4dp"
android:layout_marginRight="48dp"
android:background="@drawable/cvsn_bg_left"
android:gravity="left"
android:text="HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello"
android:textColor="#0d0d0d"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2013/07/10 11:30"
android:textColor="#0d0d0d"
android:paddingLeft="16dp"/>
</LinearLayout>
</LinearLayout>
===================================================-
2、在Log中查看短信
SmsActivity.java
package cpm.tarena.Phone;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
public class SmsActivity extends Activity{
ContentResolver mContentResolver;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mContentResolver = this.getContentResolver();
//讀出每個電話號碼,對應的最新一條短信//準備Uri 調用數據
Uri smsTitle =Uri.parse("content://mms-sms/conversations");
Cursor mCursor = mContentResolver.query(smsTitle, null, null, null, "date desc");
String[] arrcols = mCursor.getColumnNames();
while(mCursor.moveToNext()){
for(int i = 0; i<arrcols.length; i++){
Log.i("sms_data", arrcols[i]+":"+mCursor.getString(i));
}
Log.i("sms_data", "-------------------------");
}
mCursor.close();
//查詢Thread_id對應的短信
Uri smsdetaildata = Uri.parse("content://sms");
mCursor =mContentResolver.query(smsdetaildata, null, "thread_id=1", null, "date desc");
arrcols = mCursor.getColumnNames();
while(mCursor.moveToNext()){
for(int i = 0; i<arrcols.length; i++){
Log.i("sms_detaildata", arrcols[i]+":"+mCursor.getString(i));
}
Log.i("sms_detaildata", "************************");
}
mCursor.close();
}
}
在manifest 中修改name的爲.SmsActivity
創建sms_data 和 sms_detaildata 分別查看
=================================================================
3、加滾動條的從下往上顯示短信
sms_detail_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fcfcfc">
<ScrollView
android:id="@+id/sms_scrollview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#fcfcfc" >
<LinearLayout
android:id="@+id/sms_detail_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fdfdfd"
android:gravity="bottom"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="4dp"
android:background="@drawable/cvsn_bg_right"
android:text="Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2Hi2"
android:textColor="#0d0d0d"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2013/07/10 11:35"
android:textColor="#0d0d0d"
android:paddingRight="16dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginRight="24dp"
android:layout_marginTop="4dp"
android:background="@drawable/cvsn_bg_left"
android:gravity="left"
android:text="HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello"
android:textColor="#0d0d0d"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2013/07/10 11:30"
android:textColor="#0d0d0d"
android:paddingLeft="16dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginRight="24dp"
android:layout_marginTop="4dp"
android:background="@drawable/cvsn_bg_left"
android:gravity="left"
android:text="HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello"
android:textColor="#0d0d0d"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2013/07/10 11:30"
android:textColor="#0d0d0d"
android:paddingLeft="16dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginRight="24dp"
android:layout_marginTop="4dp"
android:background="@drawable/cvsn_bg_left"
android:gravity="left"
android:text="4HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello"
android:textColor="#0d0d0d"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2013/07/10 11:30"
android:textColor="#0d0d0d"
android:paddingLeft="16dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:layout_marginRight="24dp"
android:layout_marginTop="4dp"
android:background="@drawable/cvsn_bg_left"
android:gravity="left"
android:text="3HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello"
android:textColor="#0d0d0d"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2013/07/10 11:30"
android:textColor="#0d0d0d"
android:paddingLeft="16dp"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
-----------------------------------------
SmsActivity.java
package cpm.tarena.Phone;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.ScrollView;
public class SmsActivity extends Activity {
ContentResolver mContentResolver;
LinearLayout mLinearLayout;
ScrollView mScrollView;
Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
int mScrollViewHeight = mScrollView.getHeight();
int mLinearLayoutHeight = mLinearLayout.getMeasuredHeight();
int moveSizeY = mLinearLayoutHeight - mScrollViewHeight;
mScrollView.scrollTo(0, moveSizeY);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_detail_layout);
// 找到滾動條和顯示短信的佈局,計算 該移動的高度
mScrollView = (ScrollView) findViewById(R.id.sms_scrollview);
mLinearLayout = (LinearLayout) findViewById(R.id.sms_detail_layout);
// //1毫秒、10毫秒、100毫秒不行,1秒就行了(簡單推遲)
// mHandler.sendEmptyMessageDelayed(0, 1000);
mContentResolver = this.getContentResolver();
// 讀出每個電話號碼,對應的最新一條短信//準備Uri 調用數據
Uri smsTitle = Uri.parse("content://mms-sms/conversations");
Cursor mCursor = mContentResolver.query(smsTitle, null, null, null,
"date desc");
String[] arrcols = mCursor.getColumnNames();
while (mCursor.moveToNext()) {
for (int i = 0; i < arrcols.length; i++) {
Log.i("sms_data", arrcols[i] + ":" + mCursor.getString(i));
}
Log.i("sms_data", "-------------------------");
}
mCursor.close();
// 查詢Thread_id對應的短信
Uri smsdetaildata = Uri.parse("content://sms");
mCursor = mContentResolver.query(smsdetaildata, null, "thread_id=1",
null, "date desc");
arrcols = mCursor.getColumnNames();
while (mCursor.moveToNext()) {
for (int i = 0; i < arrcols.length; i++) {
Log.i("sms_detaildata", arrcols[i] + ":" + mCursor.getString(i));
}
Log.i("sms_detaildata", "************************");
}
mCursor.close();
}
@Override
protected void onResume() {
super.onResume();
// 1毫秒、10毫秒不行,100毫秒就行了(簡單推遲)
mHandler.sendEmptyMessageDelayed(0, 100);
}
}
==========================================================
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.