Android通過應(yīng)用程序創(chuàng)建快捷方式的方法
本文實(shí)例講述了Android通過應(yīng)用程序創(chuàng)建快捷方式的方法。分享給大家供大家參考。具體如下:
Android 快捷方式是桌面最基本的組件。它用于直接啟動(dòng)某一應(yīng)用程序的某個(gè)組件。
一般情況下,可以在Launcher的應(yīng)用程序列表上,通過長(zhǎng)按某一個(gè)應(yīng)用程序的圖標(biāo)在左面上創(chuàng)建改該應(yīng)用程序的快捷方式。另外,還可以通過兩種方式在桌面上添加快捷方式:
一:在應(yīng)用程序中創(chuàng)建一個(gè)Intent,然后以Broadcast的形式通知Launcher創(chuàng)建一個(gè)快捷方式。
二:為應(yīng)用程序的組件注冊(cè)某一個(gè)符合特定條件的IntentFilter,然后可以直接在Launcher的桌面添加啟動(dòng)該組件的快捷方式。
下面模擬在應(yīng)用程序中添加快捷方式
main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/createShortcut" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="20px" android:text="創(chuàng)建快捷鍵"/> <Button android:id="@+id/exit" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:textSize="20px" android:text="退出"/> </LinearLayout>
清單文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ljq.action" android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".ShortCutAction"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<!-- 添加快捷鍵權(quán)限 -->
<uses-permission
android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
</manifest>
ShortCutAction類:
package com.ljq.action;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
/**
* 通過應(yīng)用程序創(chuàng)建快捷方式
*
* @author jiqinlin
*
*/
public class ShortCutAction extends Activity implements OnClickListener{
private Button createShortcut=null; //創(chuàng)建快捷鍵按鈕
private Button exit=null;//退出系統(tǒng)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createShortcut=(Button)findViewById(R.id.createShortcut);
exit=(Button)findViewById(R.id.exit);
createShortcut.setOnClickListener(this);
exit.setOnClickListener(this);
}
public void onClick(View v) {
//Button btn=(Button)v;
switch (v.getId()) {
case R.id.createShortcut:
//String title=getResources().getString(R.string.title);
Intent addIntent=new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
Parcelable icon=Intent.ShortcutIconResource.fromContext(this, R.drawable.png); //獲取快捷鍵的圖標(biāo)
Intent myIntent=new Intent(this, ShortCutAction.class);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式");//快捷方式的標(biāo)題
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);//快捷方式的圖標(biāo)
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);//快捷方式的動(dòng)作
sendBroadcast(addIntent);//發(fā)送廣播
break;
case R.id.exit:
System.exit(0);
break;
}
}
}
運(yùn)行結(jié)果:


希望本文所述對(duì)大家的Android程序設(shè)計(jì)有所幫助。
- Android 創(chuàng)建/驗(yàn)證/刪除桌面快捷方式(已測(cè)試可用)
- android 為應(yīng)用程序創(chuàng)建桌面快捷方式技巧分享
- 解析Android應(yīng)用啟動(dòng)后自動(dòng)創(chuàng)建桌面快捷方式的實(shí)現(xiàn)方法
- Android的Launcher啟動(dòng)器中添加快捷方式及小部件實(shí)例
- Android添加(創(chuàng)建)、刪除及判斷是否存在桌面快捷方式的方法
- Android中創(chuàng)建快捷方式代碼實(shí)例
- Android實(shí)現(xiàn)向Launcher添加快捷方式的方法
- android編程實(shí)現(xiàn)為程序創(chuàng)建快捷方式的方法
- Android應(yīng)用創(chuàng)建桌面快捷方式代碼
- Android中創(chuàng)建快捷方式及刪除快捷方式實(shí)現(xiàn)方法
- Android應(yīng)用創(chuàng)建多個(gè)快捷方式
- Android編程實(shí)現(xiàn)向桌面添加快捷方式的方法
- Android編程實(shí)現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法
相關(guān)文章
淺析AndroidStudio3.0最新 Android Profiler分析器(cpu memory network
Android Profiler分為三大模塊: cpu、內(nèi)存 、網(wǎng)絡(luò)。本文給大家介紹AndroidStudio3.0最新 Android Profiler分析器(cpu memory network 分析器)的相關(guān)知識(shí),他們的基本使用方法,在文中都給大家提到,具體內(nèi)容詳情大家通過本文一起學(xué)習(xí)吧2017-12-12
Android開發(fā)中LayoutInflater用法詳解
這篇文章主要介紹了Android開發(fā)中LayoutInflater用法,結(jié)合實(shí)例形式分析了LayoutInflater類的功能、作用、使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-08-08
Android中用Builder模式自定義Dialog的方法
在任何軟件操作系統(tǒng)中,Dialog即對(duì)話框都是一種重要的交互模式與信息載體,而Android系統(tǒng)本身的Dialog擁有固定的樣式,并且在5.0后采用Material Design設(shè)計(jì)風(fēng)格的Dialog美觀大氣。這篇文章將詳細(xì)介紹Android中用Builder模式自定義Dialog的方法,有需要的可以參考借鑒。2016-10-10
Android 處理 View 重復(fù)點(diǎn)擊的多種方法
這篇文章主要介紹了Android 處理 View 重復(fù)點(diǎn)擊的多種方法,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下2021-03-03
Android自定義動(dòng)態(tài)壁紙開發(fā)(時(shí)鐘)
今天小編就為大家分享一篇關(guān)于Android自定義動(dòng)態(tài)壁紙開發(fā)(時(shí)鐘),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
ListView的Adapter使用(綁定數(shù)據(jù)) 之 自定義每一項(xiàng)的布局去綁定數(shù)據(jù)
之前寫的綁定數(shù)據(jù)是只是簡(jiǎn)單的綁定了字符串,這次我們將一次綁定多條數(shù)據(jù)并且嘗試用自定義的布局。在這篇文章中首先講解的是用Hashmap 去綁定數(shù)據(jù),第二個(gè)例子,講解自定義布局然后綁定數(shù)據(jù)2013-06-06
Android實(shí)戰(zhàn)教程第四十篇之Chronometer實(shí)現(xiàn)倒計(jì)時(shí)
這篇文章主要介紹了Android實(shí)戰(zhàn)教程第四十篇之Chronometer實(shí)現(xiàn)倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
使用IntelliJ IDEA 配置安卓(Android)開發(fā)環(huán)境的教程詳解(新手必看)
這篇文章主要介紹了使用IntelliJ IDEA 配置安卓(Android)開發(fā)環(huán)境的教程詳解(新手必看),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

