Android快捷方式Shortcut及桌面小部件AppWidget 開發(fā)

一、快捷方式

? ? 1、靜態(tài)創(chuàng)建:主要是xml

? ? ? ? (1)、在項(xiàng)目res目錄下新建xml包,在xml目錄下新建自定義名稱的xml文件,內(nèi)容參考如下:

<?xml version="1.0" encoding="utf-8"?>

<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">

? ? <!--1、shortcut節(jié)點(diǎn)可建多個(gè),但系統(tǒng)最多只會(huì)顯示4或5個(gè)-->

? ? //shortcut參數(shù):

? ? //enabled:可選、是否啟用此快捷方式 ,默認(rèn)false,

? ? //當(dāng)設(shè)為true運(yùn)行后且拖拽此快捷方式到桌面后,把此值刪除或改為false,再次運(yùn)行,桌面的快捷方式會(huì)置灰,長按后不再顯示此快捷方式選項(xiàng)

? ? // icon:可選、快捷方式的圖標(biāo)

? ? //shortcutId:必選、快捷方式的id,必須是唯一的且不能引用資源庫

? ? //shortcutShortLabel:必選、快捷方式的短名稱,10個(gè)字符串以內(nèi)且必須為資源文件strings引用,在長名稱顯示不下或者沒有長名稱的時(shí)候顯示

? ? //shortcutLongLabel:可選、快捷方式的短名稱,25個(gè)字符以內(nèi)且必須為資源文件strings引用,會(huì)優(yōu)先顯示長名稱

? ? //shortcutDisabledMessage:可選、當(dāng)此快捷被禁用,也就是快捷方式置灰時(shí)被點(diǎn)擊了,吐司的提示信息,必須為資源文件strings引用

? ? //intent參數(shù):

? ? //action:指定Intent要啟動(dòng)的操作或任務(wù),該屬性必須要指定,否則不會(huì)顯示快捷方式。

? ? //targetClass:要跳轉(zhuǎn)的目標(biāo)類。

? ? // targetPackage:要跳轉(zhuǎn)的目標(biāo)應(yīng)用包名。

? ? <shortcut

? ? ? ? android:enabled="true"

? ? ? ? android:icon="@mipmap/aa"

? ? ? ? android:shortcutDisabledMessage="@string/shortcut_disabled_message"

? ? ? ? android:shortcutId="iddddd1"

? ? ? ? android:shortcutLongLabel="@string/shortcut_long_label"

? ? ? ? android:shortcutShortLabel="@string/shortcut_short_label">

? ? ? ? <intent

? ? ? ? ? ? android:action="com.dashingqi.shortcut.Back"

? ? ? ? ? ? android:targetClass="com.mo.shortcutsdemo.StaticStateActivity"

? ? ? ? ? ? android:targetPackage="com.mo.shortcutsdemo" />

? ? ? ? <categories android:name="android.shortcut.conversation" />

? ? </shortcut>

</shortcuts>


? ? ? ? 2、動(dòng)態(tài)創(chuàng)建:主要是代碼,如下

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {

? ? ? ? ? ? // android 7.1

? ? ? ? ? ? ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

? ? ? ? ? ? Intent intent =new Intent(this, DynamicActivity.class);

? ? ? ? ? ? intent.setAction(Intent.ACTION_VIEW);

? ? ? ? ? ? ShortcutInfo shortcutInfo =new ShortcutInfo.Builder(this, "id1")

? ? ? ? ? ? ? ? ? ? .setShortLabel("動(dòng)態(tài)短標(biāo)簽")

? ? ? ? ? ? ? ? ? ? .setLongLabel("動(dòng)態(tài)長標(biāo)簽")

? ? ? ? ? ? ? ? ? ? .setIcon(Icon.createWithResource(this, R.mipmap.aa))

//? ? ? ? ? ? ? ? ? ? .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")))

? ? ? ? ? ? ? ? ? ? .setIntent(intent)

? ? ? ? ? ? ? ? ? ? .build();

? ? ? ? ? ? //ShortcutInfo 可new多個(gè)實(shí)例

? ? ? ? ? ? shortcutManager.setDynamicShortcuts(Arrays.asList(shortcutInfo));

? ? ? ? }

? ? ? ? 3、固態(tài)創(chuàng)建快捷方式:就是在程序里由代碼控制直接在桌面添加一個(gè)快捷方式,不會(huì)在APP圖標(biāo)長按的操作里顯示

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

? ? ? ? ? ? ? ? ? ? ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);

? ? ? ? ? ? ? ? ? ? if (shortcutManager.isRequestPinShortcutSupported()){//系統(tǒng)是否支持

? ? ? ? ? ? ? ? ? ? ? ? Intent intent =new Intent(MainActivity.this, FixedActivity.class);//快捷方式點(diǎn)擊后去哪

? ? ? ? ? ? ? ? ? ? ? ? intent.setAction(Intent.ACTION_VIEW);//必須:不然跳不動(dòng)

? ? ? ? ? ? ? ? ? ? ? ? ShortcutInfo shortcutInfo =new ShortcutInfo.Builder(MainActivity.this, "id2")

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .setShortLabel("固態(tài)短標(biāo)簽")

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .setLongLabel("固態(tài)長標(biāo)簽")

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .setIcon(Icon.createWithResource(MainActivity.this, R.mipmap.aa))

//? ? ? ? ? ? ? ? ? ? .setIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com")))

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .setIntent(intent)

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? .build();

? ? ? ? ? ? ? ? ? ? ? ? //添加跳轉(zhuǎn)意圖

? ? ? ? ? ? ? ? ? ? ? ? Intent pinnedShortcutCallbackIntent = shortcutManager.createShortcutResultIntent(shortcutInfo);

? ? ? ? ? ? ? ? ? ? ? ? PendingIntent successCallback = PendingIntent.getBroadcast(MainActivity.this, 0, pinnedShortcutCallbackIntent, 0);

? ? ? ? ? ? ? ? ? ? ? ? shortcutManager.requestPinShortcut(shortcutInfo,successCallback.getIntentSender());

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

二、桌面小部件:

? ? 1、小部件添加到桌面時(shí)的顯示樣式-xml,如:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:layout_width="match_parent"

android:background="@mipmap/ic_launcher"

? ? android:layout_height="match_parent">

? ? <TextView

? ? ? ? android:id="@+id/appwidget_text"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_gravity="center"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:text="小部件" />

</FrameLayout>

? ? 2、自定義繼承AppWidgetProvider的類

/**

* @ author:mo

* @ data:2021/7/28:10:21

* @ 功能:

*/

public class MyAppWidgetextends AppWidgetProvider{

? ? /**接收窗口小部件點(diǎn)擊時(shí)發(fā)送的廣播 */

? ? @Override

? ? public void onReceive(Context context, Intent intent) {

? ? ? ? super.onReceive(context, intent);

? ? }

? ? public? static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,? int appWidgetId) {

? ? ? ? //添加到桌面后對(duì)顯示視圖內(nèi)容修改

? ? ? ? CharSequence widgetText ="小部件快捷方式";

? ? ? ? RemoteViews views =new RemoteViews(context.getPackageName(), R.layout.new_app_widget);

? ? ? ? views.setTextViewText(R.id.appwidget_text, widgetText);

? ? ? ? //點(diǎn)擊事件添加

? ? ? ? Intent intent =new Intent(context, WidgetActivity.class);

? ? ? ? PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

? ? ? ? views.setOnClickPendingIntent(R.id.appwidget_text, pendingIntent);

? ? ? ? appWidgetManager.updateAppWidget(appWidgetId, views);

? ? }

? ? /** 每次窗口小部件被更新都調(diào)用一次該方法*/

? ? @Override

? ? public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

? ? ? ? super.onUpdate(context, appWidgetManager, appWidgetIds);

? ? ? ? Log.i("MyAppWidget", "開始了更新");

? ? ? ? for (int appWidgetId : appWidgetIds) {

? ? ? ? ? ? updateAppWidget(context, appWidgetManager, appWidgetId);

? ? ? ? }

}

? ? /**每刪除一次窗口小部件就調(diào)用一次 */

? ? @Override

? ? public void onDeleted(Context context, int[] appWidgetIds) {

? ? ? ? super.onDeleted(context, appWidgetIds);

? ? ? ? //context.stopService(new Intent(context, WidgetService.class));

? ? ? ? Log.i("MyAppWidget", "刪除成功!");

? ? }

? ? /**當(dāng)該窗口小部件第一次添加到桌面時(shí)調(diào)用該方法 */

? ? @Override

? ? public void onEnabled(Context context) {

? ? ? ? super.onEnabled(context);

? ? ? ? // Intent mTimerIntent = new Intent(context, WidgetService.class);

? ? ? ? Log.i("MyAppWidget", "創(chuàng)建成功!");

? ? }

? ? /**當(dāng)最后一個(gè)該窗口小部件刪除時(shí)調(diào)用該方法 */

? ? @Override

? ? public void onDisabled(Context context) {

? ? ? ? super.onDisabled(context);

? ? ? ? //? Intent mTimerIntent = new Intent(context, WidgetService.class);

// context.stopService(mTimerIntent);

? ? ? ? Log.i("AppWidget", "刪除成功!");

? ? }

? ? /** 當(dāng)小部件大小改變時(shí)*/

? ? @Override

? ? public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {

? ? ? ? super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions);

? ? }

? ? /** 當(dāng)小部件從備份恢復(fù)時(shí)調(diào)用該方法*/

? ? @Override

? ? public void onRestored(Context context, int[] oldWidgetIds, int[] newWidgetIds) {

? ? ? ? super.onRestored(context, oldWidgetIds, newWidgetIds);

? ? }

}

? ? 3、添加配置信息

<?xml version="1.0" encoding="utf-8"?>

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"

? ? android:initialLayout="@layout/new_app_widget"

? ? android:minHeight="80dp"

? ? android:minWidth="80dp"

? ? android:previewImage="@mipmap/aa"

? ? android:updatePeriodMillis="1200001"

? ? android:widgetCategory="home_screen|keyguard">


</appwidget-provider>

????<!--initialLayout: 初始化布局-->

? ? <!--minHeight/minWidth: 寬高? 40dp等于一個(gè)-->

? ? <!--previewImage: 選擇添加小部件時(shí),小部件的圖標(biāo)-->

? ? <!--updatePeriodMillis: 數(shù)據(jù)更新周期,單位毫秒,起步為30分鐘-->

? ? <!--resizeMode: 調(diào)整大小模式也就是拉伸模式-->

? ? 4、清單里進(jìn)行注冊(cè)

<receiver android:name=".MyAppWidget" >

? ? <intent-filter>

? ? ? ? <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />

? ? </intent-filter>

? ? <meta-data

? ? ? ? android:name="android.appwidget.provider"

? ? ? ? android:resource="@xml/my_app_widget_info" />

</receiver>

? ??

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容