AnyCat 是android一個小的項目,雖然麻雀很小但是知識很多。別看小程序就沒學習的地方,錯也。現將學習總結如下:以下為AnyCat項目精髓之處。
轉載請注明:QQ:273733055 ppwuyi@sohu.com
程序核心代碼如下:
http://hi.baidu.com/adnroidorg/blog/item/b79194a4c424809bd14358ba.html
/****
* result.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
* result.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); //-----------廣播接收者
* result.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent); //---------------加載進去
* result.putExtra(Intent.EXTRA_SHORTCUT_NAME, info.loadLabel(mPackageManager));//------------獲取名字 地址路徑
* result.putExtra(Intent.EXTRA_SHORTCUT_ICON, bd.getBitmap());//----------------加載進去
*/
sendBroadcast(result); //注意這才是創建快接方式的核心地點
將裝配好的Intent通過廣播的形式發送到com.android.launcher.action.INSTALL_SHORTCUT;
不過中小功能的地方我們也學習到不少,現總結如下:
1. FrontDoorActivity.java 無學習要點。
2 .ActivityPickerActivity.java 學習到怎么獲取系統應用程序的程序名和路徑。
view.setTag(view.findViewById(android.R.id.text1));
final TextView textView = (TextView) view.getTag();
Intent queryIntent = new Intent(Intent.ACTION_MAIN); //過濾地址
List<ResolveInfo> list = mPackageManager.queryIntentActivities(queryIntent, 0); //返回符合 地址要求的列表
textView.setText(info.getInfo().loadLabel(mPackageManager)); //取出項目名
mPackageManager = getPackageManager();//獲取包
//獲取應用程序的圖標
Drawable drawable = info.loadIcon(mPackageManager); //---------------------------讀取程序圖標
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bd = (BitmapDrawable) drawable;
result.putExtra(Intent.EXTRA_SHORTCUT_ICON, bd.getBitmap());//----------------加載進去
}
//獲取鏈接應用程序的橋梁
Intent intent = new Intent();//準備
intent.setComponent(new ComponentName(info.activityInfo.applicationInfo.packageName,
info.activityInfo.name)); //這是核心
轉載請注明:QQ:273733055 ppwuyi@sohu.com
3.CreateShortcutActivity.java 學習到很多東西
//獲取單一的聯系人
Intent intent = new Intent(Intent.ACTION_PICK, Phones.CONTENT_URI); //顯示聯系人列表并重中選擇一個聯系人的URI鏈接方式
intent.putExtra(Contacts.Intents.UI.TITLE_EXTRA_KEY, getText(R.string.callShortcutActivityTitle)); //傳遞一個標題給聯系人列表
startActivityForResult(intent, REQUEST_PHONE);//開啟聯系人選擇的activity等待用戶選擇
//別樣的構造函數
return new ShortcutEditorDialog(this, this, this); //注意這個this,this,this 應為實現了onClick,onCancel接口
public ShortcutEditorDialog(Context context, OnClickListener onClick, OnCancelListener onCancel){.....}
//很和諧的代碼
int iconSize = (int) r.getDimension(android.R.dimen.app_icon_size);//獲得當前系統快捷方式的圖片的限制大小
Bitmap icon = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888); //建立一個空的BItMap
Canvas canvas = new Canvas(icon);//初始化畫布 繪制的圖像到icon上
// Copy in the photo
Paint photoPaint = new Paint(); //建立畫筆
photoPaint.setDither(true); //獲取跟清晰的圖像采樣
photoPaint.setFilterBitmap(true);//過濾一些
Rect src = new Rect(0, 0, photo.getWidth(), photo.getHeight());//創建一個指定的新矩形的坐標
Rect dst = new Rect(0, 0, iconSize, iconSize);//創建一個指定的新矩形的坐標
canvas.drawBitmap(photo, src, dst, photoPaint);//將photo 縮放或則擴大到 dst使用的填充區photoPaint
//再次被和諧的代碼
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//設置畫筆
textPaint.setTextSize(20.0f);//字體大小
textPaint.setTypeface(Typeface.DEFAULT_BOLD);//采用默認的寬度
textPaint.setColor(r.getColor(R.color.textColorIconOverlay));//采用的顏色
textPaint.setShadowLayer(3f, 1, 1, r.getColor(R.color.textColorIconOverlayShadow));//影音的設置
canvas.drawText(overlay, 2, 16, textPaint);//繪制上去 字,開始未知x,y采用那只筆繪制
4.ShortcutEditorDialog.java
唯一學習到了一點是
mNameView.setError(getContext().getText(R.string.errorEmptyName));//顯示輸入框為空的告警
想看效果自己動手吧。