日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 为你的应用程序添加快捷方式【优先级高的快捷方式】

發(fā)布時間:2023/12/19 Android 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 为你的应用程序添加快捷方式【优先级高的快捷方式】 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

有人會說,快捷方式,不是安裝完應(yīng)用程序后,長按應(yīng)用程序的ICON然后將它拖到桌面上不就行了嗎?沒錯,這樣是一種方法,但這種方法有一個缺點,看圖吧:

如上圖,如果我們長按桌面點擊快捷方式,將會跳到如下界面,如果單從這個界面選擇的話,我們就必須進(jìn)入Applications 目錄,然后再在Applications 里面選擇我們對應(yīng)的應(yīng)用程序,這樣的話用戶可能得麻煩的去找咯。但我們同時會發(fā)現(xiàn),在Applications 的下面有很多另外的ICON比如 上圖的BookMark ,Contact 等,這些也是應(yīng)用,那么這些是怎么做到不用進(jìn)去Applications 而在第一頁就出現(xiàn)供用戶選擇呢?今天我們就針對這點來講講吧。

?

要做這一功能首先我們先來了解一下manifest 里面的這一標(biāo)簽:

<activity-alias>

?

syntax:語法: <activity-alias android:=["true" | "false"]
android:=["true" | "false"]
android:="drawable resource"
android:="string resource"
android:="string"
android:="string"
android:="string" >
. . .
</activity-alias> contained in:隸屬于: <application> can contain:可以包含: <intent-filter>
<meta-data> description:說明: An alias for an activity, named by the targetActivity attribute. The target must be in the same application as the alias and it must be declared before the alias in the manifest.
activity的一個別名,用targetActivity屬性命名。目標(biāo)activity必須與別名在同一應(yīng)用程序的manifest里,并且在別名之前聲明。

The alias presents the target activity as a independent entity. It can have its own set of intent filters, and they, rather than the intent filters on the target activity itself, determine which intents can activate the target through the alias and how the system treats the alias. For example, the intent filters on the alias may specify the "android.intent.action.MAIN" and "android.intent.category.LAUNCHER" flags, causing it to be represented in the application launcher, even though none of the filters on the target activity itself set these flags.
別名作為一個獨立的實體代表目標(biāo)activity。它可以有它自己的一套intent filter,它們,而不是目標(biāo)activity自己的intent filter,決定哪個intent能夠活性化目標(biāo)通過別名以及系統(tǒng)如何處理別名。例如,別名的intent filter可以指定"android.intent.action.MAIN"和"android.intent.category.LAUNCHER"標(biāo)簽,使之顯示在應(yīng)用程序啟動器上,即使目標(biāo)activity自己沒有設(shè)置這些標(biāo)簽。

With the exception of targetActivity, <activity-alias> attributes are a subset of <activity> attributes. For attributes in the subset, none of the values set for the target carry over to the alias. However, for attributes not in the subset, the values set for the target activity also apply to the alias.
targetActivity的例外,<activity-alias>屬性是<activity>屬性的一個子集。對于該子集中的屬性,目標(biāo)activity中設(shè)置的值不會覆蓋別名的值。然而,對于那些子集中沒有設(shè)置的屬性,設(shè)置給目標(biāo)activity的值同樣適用于別名。

?

上面給出的解釋我們來配置一下manifest,配置為如下:

?

<activity?android:name=".shortcut">
????????????
<intent-filter>
????????????????
<action?android:name="android.intent.action.MAIN"?/>
????????????
</intent-filter>
????????
</activity>

????????
<activity-alias?android:name=".CreateShortcuts"
????????????android:targetActivity
=".shortcut"?android:label="@string/shortcut">

????????????
<intent-filter>
????????????????
<action?android:name="android.intent.action.CREATE_SHORTCUT"?/>
????????????????
<category?android:name="android.intent.category.DEFAULT"?/>
????????????
</intent-filter>

????????
</activity-alias>

?

?

Activity:

.shortcut 是我們快捷方式需要的Activity

activity-alias:

?對應(yīng)的targetActivity是指向應(yīng)用創(chuàng)建快捷方式使用的Activity

android:label對應(yīng)的創(chuàng)建快捷方式列表顯示的文字,而該應(yīng)用對應(yīng)的快捷方式的圖標(biāo)則默認(rèn)使用我們給定的application的圖標(biāo)。如圖:

好了,這是第一步步驟,下面進(jìn)入代碼階段,先看代碼:

?

package?com.terry.attrs;

import?android.app.Activity;
import?android.content.Intent;
import?android.os.Bundle;
import?android.os.Parcelable;
import?android.widget.LinearLayout;
import?android.widget.TextView;

public?class?shortcut?extends?Activity?{
????
private?static?final?String?SHORT_CUT_EXTRAS?=?"com.terry.extra.short";

????@Override
????
protected?void?onCreate(Bundle?savedInstanceState)?{
????????
//?TODO?Auto-generated?method?stub
????????super.onCreate(savedInstanceState);
????????final?Intent?intent?
=?getIntent();
????????final?String?action?
=?intent.getAction();
????????
if?(Intent.ACTION_CREATE_SHORTCUT.equals(action))?{
????????????createShortCut();
????????????finish();
????????????
return;
????????}

????????String?extra?
=?intent.getStringExtra(SHORT_CUT_EXTRAS);
????????LinearLayout?layout?
=?new?LinearLayout(getApplicationContext());
????????TextView?tv?
=?new?TextView(getApplicationContext());

????????
if?(extra?!=?null)
????????????tv.setText(extra);
????????layout.addView(tv);
????????setContentView(layout);
????}

????
void?createShortCut()?{
????????Intent?shortcutIntent?
=?new?Intent(Intent.ACTION_MAIN);
????????shortcutIntent.setClass(
this,?this.getClass());
????????shortcutIntent.putExtra(SHORT_CUT_EXTRAS,?
"測試的快捷方式");

????????Intent?intent?
=?new?Intent();
????????intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,?shortcutIntent);
????????intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,?
"這里隨便指定");
????????Parcelable?shortIcon?
=?Intent.ShortcutIconResource.fromContext(
????????????????
this,?com.terry.attrs.R.drawable.icon);
????????intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,?shortIcon);
????????setResult(RESULT_OK,?intent);
????}
}

?

?

代碼解釋:

onCreate方法,首先獲取intent 的action如果接收到的action為創(chuàng)建快捷方式的請求,則執(zhí)行創(chuàng)建快捷方式的代碼,否則則通過得到的extra 為textView 賦值。

createShortCut方法,首先設(shè)置快捷方式點擊后要跳轉(zhuǎn)的intent 和要帶入的參數(shù),然后設(shè)置桌面快捷方式的名稱,圖標(biāo)和對應(yīng)的intent(即上面帶入數(shù)據(jù)和跳轉(zhuǎn)的界面的 class的Intent)最后將結(jié)果傳入。

最近運行的結(jié)果:

跳擊后到達(dá)的界面:

?

TIP:這里可以是任何ACTIVITY界面。

最后給大家分享下源碼吧:

快捷方式

就到這里,希望我的一篇廢話能對你有所幫助。

?

總結(jié)

以上是生活随笔為你收集整理的Android 为你的应用程序添加快捷方式【优先级高的快捷方式】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。