編輯:關於Android編程
Intent源碼詳解,直接開始入題:
Intent源碼6700多行代碼,但真正核心代碼 就那麼幾百行,大部分都用來定義常量字符串了
先來看一下
public class Intent implements Parcelable, Cloneable
沒錯,它還實現了cloneable接口,但平常我們很少會用到它,其實現方法為:
/**
* Copy constructor.
*/
public Intent(Intent o) {
this.mAction = o.mAction;
this.mData = o.mData;
this.mType = o.mType;
this.mPackage = o.mPackage;
this.mComponent = o.mComponent;
this.mFlags = o.mFlags;
if (o.mCategories != null) {
this.mCategories = new HashSet(o.mCategories);
}
if (o.mExtras != null) {
this.mExtras = new Bundle(o.mExtras);
}
if (o.mSourceBounds != null) {
this.mSourceBounds = new Rect(o.mSourceBounds);
}
if (o.mSelector != null) {
this.mSelector = new Intent(o.mSelector);
}
if (o.mClipData != null) {
this.mClipData = new ClipData(o.mClipData);
}
}
@Override
public Object clone() {
return new Intent(this);
}
沒錯,就是復制了一個自己
Intent另一個功能,就是傳參,這裡只展示public Intent putExtra(String name, String value),其他的都是類似這個寫法:
public Intent putExtra(String name, String value) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putString(name, value);
return this;
}
其中,mExtras是private Bundle mExtras;
那麼,這裡還得說一下public Intent putExtras(Bundle extras)方法:
public Intent putExtras(Bundle extras) {
if (mExtras == null) {
mExtras = new Bundle();
}
mExtras.putAll(extras);
return this;
}
沒錯,bundle中放入一個bundle。。。
再看一下取出的方法:
public String getStringExtra(String name) {
return mExtras == null ? null : mExtras.getString(name);
}
小結:如果是基本數據類型(Int,long,double,boolean,string)傳參,沒必要自己生成一個bundle對象,再傳參到Intent中。
接下來,聊聊 parseUri 這個方法,裡面傳的參數是 uri,但到裡面,基本就是按照字符串解析
隨便摘取幾段
// simple case
i = uri.lastIndexOf("#");
if (i == -1) return new Intent(ACTION_VIEW, Uri.parse(uri));
// old format Intent URI
if (!uri.startsWith("#Intent;", i)) return getIntentOld(uri);
另一個被忽略的是toString()方法,Intent的重寫了該方法,可以通過輸出String查看Intnet裡具體包含的信息內容
@Override
public String toString() {
StringBuilder b = new StringBuilder(128);
b.append("Intent { ");
toShortString(b, true, true, true, false);
b.append(" }");
return b.toString();
}
那麼toShortString()這個方法,到底輸出了哪些內容?
可以通過傳參的名字了解一二,
public void toShortString(StringBuilder b, boolean secure, boolean comp, boolean extras,
boolean clip) {
secure對應的是 uri的輸出,
comp對應的是ComponentName 也就是要啟動的類名,
extras對應的就是傳參內容。
小結,如果在調試過程的時候,可以試著輸出Intent內容,看看是否是傳參錯誤導致的異常
手機QQ快速取消圖標上未讀消息的數字
手機QQ快速取消圖標上未讀消息的數字。現在手機QQ5.0也開始跟iso的一樣。在圖表顯示未讀消息的數字。我們進入手機QQ的時候,想取消那個未讀的顯示數字,就
Android控件之RatingBar自定義星級評分樣式
一、RatingBar簡單介紹RatingBar是基於SeekBar(拖動條)和ProgressBar(狀態條)的擴展,用星形來顯示等級評定,在使用默認RatingBar
Android-做個性化的滾動條
1.案例效果圖 2.准備素材 progress1.png(78*78)
Android中給按鈕同時設置背景和圓角示例代碼
前言最近在做按鈕的時候遇到在給按鈕設置一張圖片作為背景的同時還要自己定義圓角,最簡單的做法就是直接切張圓角圖作為按鈕就可以了,但是如果不這樣該怎麼辦呢,看代碼:下面來看效