從圖上可以看出,Android大致分7步完成快捷方式的創建:
**第1步:**Android系統的launcher程序會調用它的pickShortcut()方法去啟動系統的pickActivity程序(應用);
**第2步:**pickActivity程序(應用)啟動后會調用它的CheckIntentFilter()方法,去在系統中尋找可以創建快捷方式的應用有哪些,并且列舉出來。只要第三方 App用標簽進行了相應的注冊(具體如何注冊請看下面的代碼)就可以被發現并列舉出來;
第3步:調用Choseitem()方法選擇創建誰的快捷方式;
第4步:完成第三步之后,pickActivity程序(應用)會將選擇的消息通過Intent返回給系統的launcher;
**第5步:**launcher程序獲得pickActivity返回的消息后,就會知道創建誰的快捷方式,通過調用PRocessShortcut()方法去啟動第三方App中負責創建快捷方式 的Activity,這個Activity就是第二步中我們提到的用標簽進行了注冊的Activity;
第6步:第三方App中負責創建快捷方式的Activity會將快捷方式的名稱,圖標和點擊后跳轉路徑通過Intent返回給launcher;
**第7部:**launcher收到返回的消息后調用本身的ComPleteAddShortcut()方法完成快捷方式的創建,并顯示在桌面上;
這個方法一般放在引導頁面
private void createShortCut() { boolean b = SharedPreferencesTool.getBoolean(this, Constants.SHORTCUT, false); if (!b) { Intent intent = new Intent(); intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); //通過intent告訴launcher快捷方式的細節 intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "淘寶");//設置快捷方式的名稱 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);//設置快捷方式的圖標 Intent value = new Intent(this,SplashActivity.class); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, value);//設置快捷方式的操作 sendBroadcast(intent); //如果創建了快捷方式,保存一個標示,表示快捷方式創建 SharedPreferencesTool.saveBoolean(this, Constants.SHORTCUT, true); } }或者 用標簽進行注冊
<activity android:name=".CreatShortCut"> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT"/> </intent-filter></activity>向Launcher返回相關數據
public class CreatShortCut extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)) { Intent _returnIntent = new Intent(); _returnIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "csx");// 快捷鍵的名字 _returnIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,// 快捷鍵的ico Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher)); _returnIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(this, MainActivity.class));// 快捷鍵的跳轉Intent setResult(RESULT_OK, _returnIntent);// 發送 finish();// 關閉本Activity } }}引用外置的數據庫,在引導頁面初始化
private void copyDb(String fileName) { File file = new File(getFilesDir(), fileName); if (!file.exists()) { //1.獲取assets目錄的管理者 assets = getAssets(); InputStream in = null; FileOutputStream out = null; try { //2.讀取資源 in = assets.open(fileName);//打開assets目錄的資源,fileName:資源的名稱 //getCacheDir():data/data/應用程序包名/cache //getFilesDir():data/data/應用程序包名/files out = new FileOutputStream(file); //3.讀寫操作,實現拷貝 byte[] b = new byte[1024];//緩存區域 int len = -1;//保存讀取長度 while((len = in.read(b)) != -1){ out.write(b, 0, len); } } catch (IOException e) { e.printStackTrace(); }finally{ //關流操作 if (out != null) { try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (in != null) { try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }新聞熱點
疑難解答