從圖上可以看出,Android大致分7步完成快捷方式的創(chuàng)建:
**第1步:**Android系統(tǒng)的launcher程序會(huì)調(diào)用它的pickShortcut()方法去啟動(dòng)系統(tǒng)的pickActivity程序(應(yīng)用);
**第2步:**pickActivity程序(應(yīng)用)啟動(dòng)后會(huì)調(diào)用它的CheckIntentFilter()方法,去在系統(tǒng)中尋找可以創(chuàng)建快捷方式的應(yīng)用有哪些,并且列舉出來。只要第三方 App用標(biāo)簽進(jìn)行了相應(yīng)的注冊(cè)(具體如何注冊(cè)請(qǐng)看下面的代碼)就可以被發(fā)現(xiàn)并列舉出來;
第3步:調(diào)用Choseitem()方法選擇創(chuàng)建誰的快捷方式;
第4步:完成第三步之后,pickActivity程序(應(yīng)用)會(huì)將選擇的消息通過Intent返回給系統(tǒng)的launcher;
**第5步:**launcher程序獲得pickActivity返回的消息后,就會(huì)知道創(chuàng)建誰的快捷方式,通過調(diào)用PRocessShortcut()方法去啟動(dòng)第三方App中負(fù)責(zé)創(chuàng)建快捷方式 的Activity,這個(gè)Activity就是第二步中我們提到的用標(biāo)簽進(jìn)行了注冊(cè)的Activity;
第6步:第三方App中負(fù)責(zé)創(chuàng)建快捷方式的Activity會(huì)將快捷方式的名稱,圖標(biāo)和點(diǎn)擊后跳轉(zhuǎn)路徑通過Intent返回給launcher;
**第7部:**launcher收到返回的消息后調(diào)用本身的ComPleteAddShortcut()方法完成快捷方式的創(chuàng)建,并顯示在桌面上;
這個(gè)方法一般放在引導(dǎo)頁面
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快捷方式的細(xì)節(jié) intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "淘寶");//設(shè)置快捷方式的名稱 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, bitmap);//設(shè)置快捷方式的圖標(biāo) Intent value = new Intent(this,SplashActivity.class); intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, value);//設(shè)置快捷方式的操作 sendBroadcast(intent); //如果創(chuàng)建了快捷方式,保存一個(gè)標(biāo)示,表示快捷方式創(chuàng)建 SharedPreferencesTool.saveBoolean(this, Constants.SHORTCUT, true); } }或者 用標(biāo)簽進(jìn)行注冊(cè)
<activity android:name=".CreatShortCut"> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT"/> </intent-filter></activity>向Launcher返回相關(guān)數(shù)據(jù)
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));// 快捷鍵的跳轉(zhuǎn)Intent setResult(RESULT_OK, _returnIntent);// 發(fā)送 finish();// 關(guān)閉本Activity } }}引用外置的數(shù)據(jù)庫(kù),在引導(dǎo)頁面初始化
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/應(yīng)用程序包名/cache //getFilesDir():data/data/應(yīng)用程序包名/files out = new FileOutputStream(file); //3.讀寫操作,實(shí)現(xiàn)拷貝 byte[] b = new byte[1024];//緩存區(qū)域 int len = -1;//保存讀取長(zhǎng)度 while((len = in.read(b)) != -1){ out.write(b, 0, len); } } catch (IOException e) { e.printStackTrace(); }finally{ //關(guān)流操作 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(); } } } } }新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注