Android通過應(yīng)用程序創(chuàng)建快捷方式的方法
本文實例講述了Android通過應(yīng)用程序創(chuàng)建快捷方式的方法。分享給大家供大家參考。具體如下:
Android 快捷方式是桌面最基本的組件。它用于直接啟動某一應(yīng)用程序的某個組件。
一般情況下,可以在Launcher的應(yīng)用程序列表上,通過長按某一個應(yīng)用程序的圖標在左面上創(chuàng)建改該應(yīng)用程序的快捷方式。另外,還可以通過兩種方式在桌面上添加快捷方式:
一:在應(yīng)用程序中創(chuàng)建一個Intent,然后以Broadcast的形式通知Launcher創(chuàng)建一個快捷方式。
二:為應(yīng)用程序的組件注冊某一個符合特定條件的IntentFilter,然后可以直接在Launcher的桌面添加啟動該組件的快捷方式。
下面模擬在應(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); //獲取快捷鍵的圖標
Intent myIntent=new Intent(this, ShortCutAction.class);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "快捷方式");//快捷方式的標題
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);//快捷方式的圖標
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent);//快捷方式的動作
sendBroadcast(addIntent);//發(fā)送廣播
break;
case R.id.exit:
System.exit(0);
break;
}
}
}
運行結(jié)果:


希望本文所述對大家的Android程序設(shè)計有所幫助。
- Android 創(chuàng)建/驗證/刪除桌面快捷方式(已測試可用)
- android 為應(yīng)用程序創(chuàng)建桌面快捷方式技巧分享
- 解析Android應(yīng)用啟動后自動創(chuàng)建桌面快捷方式的實現(xiàn)方法
- Android的Launcher啟動器中添加快捷方式及小部件實例
- Android添加(創(chuàng)建)、刪除及判斷是否存在桌面快捷方式的方法
- Android中創(chuàng)建快捷方式代碼實例
- Android實現(xiàn)向Launcher添加快捷方式的方法
- android編程實現(xiàn)為程序創(chuàng)建快捷方式的方法
- Android應(yīng)用創(chuàng)建桌面快捷方式代碼
- Android中創(chuàng)建快捷方式及刪除快捷方式實現(xiàn)方法
- Android應(yīng)用創(chuàng)建多個快捷方式
- Android編程實現(xiàn)向桌面添加快捷方式的方法
- Android編程實現(xiàn)創(chuàng)建,刪除,判斷快捷方式的方法
相關(guān)文章
android中TabHost的圖標(48×48)和文字疊加解決方法
開發(fā)過程中,有時候圖標稍微大點,比如48×48的時候,文字就會和圖標疊加起來,遇到這種問題我們該怎樣處理呢?本文將詳細介紹希望對你有所幫助2013-01-01
Android 實現(xiàn)秒轉(zhuǎn)換成時分秒的方法
這篇文章主要介紹了Android 實現(xiàn)秒轉(zhuǎn)換成時分秒的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Android自定義wheelview實現(xiàn)滾動日期選擇器
這篇文章主要為大家詳細介紹了Android自定義wheelview實現(xiàn)滾動日期選擇器,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07

