建立案頭快捷表徵圖

來源:互聯網
上載者:User

標籤: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"/>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.