Android開發(fā)中創(chuàng)建桌面快捷方式的方法有哪些

今天就跟大家聊聊有關(guān)Android開發(fā)中創(chuàng)建桌面快捷方式的方法有哪些,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

江陽(yáng)ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來(lái)市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!

Android在桌面上生成快捷方式有兩種情況,一種是直接在桌面直接生成;一種是長(zhǎng)按桌面,在彈出的快捷菜單中生成。

談?wù)勗谧烂嫔现苯由?。個(gè)人覺(jué)得這個(gè)比較爽快,既然都是快捷方式了干嘛還要再隱藏一層呢?當(dāng)然喜歡桌面干凈的就比較喜歡第二個(gè)了。

第一個(gè)是通過(guò)廣播(Broadcast)的形式向Luncher發(fā)送請(qǐng)求生成快捷方式的。

在網(wǎng)上找到關(guān)于這方面的注冊(cè)信息。

<!--設(shè)置wallpapaer的activity -->
<!-- Intent received used to install shortcuts from other applications -->
<receiver
  android:name="com.android.launcher2.InstallShortcutReceiver"
  android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">
  <intent-filter>
    <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />
  </intent-filter>
</receiver>

可以看出,要在桌面上創(chuàng)建快捷方式就需要權(quán)限了:

android:permission="com.android.launcher.permission.INSTALL_SHORTCUT。

所以在我們的manifest.xml文件中,我們需要加入下面這段話:

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>

下面就是代碼層的實(shí)現(xiàn):

假如我在一個(gè)activity中創(chuàng)建一個(gè)創(chuàng)建快捷方式的方法:createShortCut();

public void createShortCut(){
//創(chuàng)建快捷方式的Intent
    Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    //不允許重復(fù)創(chuàng)建
    shortcutintent.putExtra("duplicate", false);
    //需要現(xiàn)實(shí)的名稱
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
    //快捷圖片
    Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
    //點(diǎn)擊快捷圖片,運(yùn)行的程序主入口
    shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , EnterActivity.class));
    //發(fā)送廣播。OK
    sendBroadcast(shortcutintent);
}

二、長(zhǎng)按桌面彈出的桌面快捷方式創(chuàng)建

第一頁(yè)談過(guò)直接在桌面生成快捷方式,現(xiàn)在說(shuō)說(shuō)如何在添加到一個(gè)SHORTCUTS列表中,就是你長(zhǎng)按桌面彈出來(lái)的那個(gè)東東。

首先在注冊(cè)activity時(shí),需要添加一個(gè)action為android.intent.action.CREATE_SHOERTCUT的intentFilter.如下所示:

<activity android:name="ShortCutTest">
  <intent-filter>
    <action android:name="android.intent.action.CREATE_SHORTCUT"/>
  </intent-filter>
</activity>

接下來(lái)就是就是設(shè)置快捷方式的圖標(biāo)、名稱、事件等屬性。這里圖表的生成,android里提供了專門的方法來(lái)生成。

public class ShortCutTest extends Activity{
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
  }
  public void createShortCut(){
    Intent addShortCut;
    //判斷是否需要添加快捷方式
    if(getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)){
      addShortCut = new Intent();
      //快捷方式的名稱
      addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME , "我的快捷方式");
      //顯示的圖片
      Parcelable icon = ShortcutIconResource.fromContext(this, R.drawable.icon);
      addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
      //快捷方式激活的activity,需要執(zhí)行的intent,自己定義
      addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent());
      //OK,生成
      setResult(RESULT_OK, addShortCut);
    }else{
      //取消
      setResult(RESULT_CANCELED);
    }
  }
}

看完上述內(nèi)容,你們對(duì)Android開發(fā)中創(chuàng)建桌面快捷方式的方法有哪些有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝大家的支持。

當(dāng)前文章:Android開發(fā)中創(chuàng)建桌面快捷方式的方法有哪些
標(biāo)題鏈接:http://muchs.cn/article6/ijogig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)網(wǎng)站營(yíng)銷、App設(shè)計(jì)動(dòng)態(tài)網(wǎng)站、微信公眾號(hào)網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)