標籤:android style color get 使用 width
分析傳統型程式的源碼發現具有接收建立案頭快捷表徵圖的廣播接受,建立快捷突變即發送廣播的方式來實現的。下面來分析建立案頭快捷表徵圖的過程。 1. 在應用程式的第一個Activity,添加建立快捷表徵圖的方法, installShortCut();
// 建立案頭快捷表徵圖
private void intallShotCut() { // 定義廣播通知案頭建立快捷表徵圖
Intent intent = new Intent();
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");// 指定意圖 // 設定動作(捷徑的名稱、表徵圖)
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "小護衛");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.safe));
// 設定開啟意圖
Intent homeIntent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, homeIntent);
sendBroadcast(intent);// 發送建立快捷表徵圖的廣播
}
記得添加許可權
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
2. 發現建立案頭快捷表徵圖後並
沒有綁定開啟我們的程式,什麼原因呢?
因為案頭應用和我們的程式是不同的應用程式,說以不能使用顯式意圖,只能使用隱式意圖。
案頭Activity的資訊清單檔作如下配置:
<activity android:name="com.itheima.mobilesafe.activities.HomeActivity" >
<intent-filter>
<action android:name="com.itheima.mobilesafe.home" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
installShortCut();修改為:
// 建立案頭快捷表徵圖
private void intallShotCut() { // 定義廣播通知案頭建立快捷表徵圖
Intent intent = new Intent();
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");// 指定意圖 // 設定動作(捷徑的名稱、表徵圖)
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "小護衛");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.safe));
// 設定隱式開啟意圖
Intent homeIntent = new Intent();
homeIntent.setAction("com.itheima.mobilesafe.home"); homeIntent.addCategory("android.intent.category.DEFAULT"); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, homeIntent);
sendBroadcast(intent);// 發送建立快捷表徵圖的廣播
}
3. 但是有一個小BUG,每次進入程式都會在案頭建立一個快捷表徵圖。記錄住是否第一次運行程式最終代碼清單如下:
// 建立案頭快捷表徵圖
private void intallShotCut() { boolean installedshortcut = sp.getBoolean("installedshortcut", false); if(installedshortcut) {return;
}
// 定義廣播通知案頭建立快捷表徵圖
Intent intent = new Intent();
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");// 指定意圖// 設定動作(捷徑的名稱、表徵圖)
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "小護衛");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, BitmapFactory.decodeResource(getResources(), R.drawable.safe));
// 設定隱式開啟意圖
Intent homeIntent = new Intent();
homeIntent.setAction("com.itheima.mobilesafe.home");homeIntent.addCategory("android.intent.category.DEFAULT");intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, homeIntent);
sendBroadcast(intent);// 發送建立快捷表徵圖的廣播
// 儲存安裝快捷表徵圖資訊
Editor editor = sp.edit();
editor.putBoolean("installedshortcut", true);editor.commit();
}
案頭Activity的資訊清單檔作如下配置:
<activity android:name="com.itheima.mobilesafe.activities.HomeActivity" >
<intent-filter>
<action android:name="com.itheima.mobilesafe.home" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
記得添加許可權
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>