Android中創建和刪除快捷方式示例 – Android移動開發技術文章_手機開發 Android移動開發教學課程

Android中創建和刪除快捷方式示例 Android系統中支持快捷方式這一特性,這樣使得用戶更快更好地使用應用或者軟件,當然,有些系統提供瞭直接拖拽創建快捷方式的功能,這裡我們將簡單介紹一下android中使用代碼如何創建和刪除快捷方式。 以下源碼來源於互聯網,本人稍作修改和註釋。原理主要是使用android系統的一個一個廣播機制,我們需要做的是實例一個Intent對象,並對這個對象做制定的設置,然後發送一個廣播就可以瞭,然後其他的操作,委托給別的程序做就可以瞭,就是這麼簡單。源碼如下。

1. package ps.androidyue.shortcutdemo; 
2.  
3. import android.app.Activity; 
4. import android.content.ComponentName; 
5. import android.content.Intent; 
6. import android.content.Intent.ShortcutIconResource; 
7. import android.os.Bundle; 
8. import android.view.View; 
9. import android.widget.Button; 
10.  
11. public class ShortCutDemoActivity extends Activity { 
12.     //安裝和卸載快捷方式動作常量  
13.     private final String INTENT_ACTION_INSTALL_SHORTCUT="com.android.launcher.action.INSTALL_SHORTCUT"; 
14.     private final String INTENT_ACTION_UNINSTALL_SHORTCUT="com.android.launcher.action.UNINSTALL_SHORTCUT"; 
15.     private Button btnAddShortCut,btnRemoveShortCuts; 
16.     /** Called when the activity is first created. */ 
17.     @Override 
18.     public void onCreate(Bundle savedInstanceState) { 
19.         super.onCreate(savedInstanceState); 
20.         setContentView(R.layout.main); 
21.         this.initializeViews(); 
22.     } 
23.      
24.     /*
25.      * 初始化需要的視圖
26.      */ 
27.     private void initializeViews(){ 
28.         this.initializeButtons(); 
29.     } 
30.      
31.     /*
32.      * 初始化需要的button
33.      */ 
34.     private void initializeButtons(){ 
35.         this.btnAddShortCut=(Button)this.findViewById(R.id.btnAddShortCuts); 
36.         this.btnAddShortCut.setOnClickListener(new View.OnClickListener() { 
37.              
38.             @Override 
39.             public void onClick(View v) { 
40.                 //添加快捷方式  
41.                 addShortcut();   
42.             } 
43.         }); 
44.         this.btnRemoveShortCuts=(Button)this.findViewById(R.id.btnRemoveShortCuts); 
45.         this.btnRemoveShortCuts.setOnClickListener(new View.OnClickListener() { 
46.              
47.             @Override 
48.             public void onClick(View v) { 
49.                 //刪除快捷方式  
50.                 delShortcut(); 
51.             } 
52.         }); 
53.     } 
54.     
55.      
56.     /*
57.      * 為程序創建桌面快捷方式
58.      */ 
59.     private void addShortcut(){ 
60.         //實例化具有安裝快捷方式動作的Intent對象  
61.         Intent shortcut = new Intent(this.INTENT_ACTION_INSTALL_SHORTCUT); 
62.         //快捷方式的名稱  
63.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name)); 
64.         shortcut.putExtra("duplicate", false); //不允許重復創建  
65.              
66.         //指定當前的Activity為快捷方式啟動的對象: 如 com.everest.video.VideoPlayer  
67.         //註意: ComponentName的第二個參數必須加上點號(.),否則快捷方式無法啟動相應程序  
68.         ComponentName comp = new ComponentName(this.getPackageName(), "."+this.getLocalClassName()); 
69.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp)); 
70.         //快捷方式的圖標  
71.         ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher); 
72.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); 
73.         //發送創建快捷方式的廣播     
74.         sendBroadcast(shortcut); 
75.     } 
76.      
77.     /*
78.      * 刪除程序的快捷方式    
79.      */     
80.     private void delShortcut(){      
81.         Intent shortcut = new Intent(this.INTENT_ACTION_UNINSTALL_SHORTCUT);      
82.                   
83.         //快捷方式的名稱       www.aiwalls.com
84.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));      
85.                   
86.         //指定當前的Activity為快捷方式啟動的對象: 如 com.everest.video.VideoPlayer       
87.         //註意: ComponentName的第二個參數必須是完整的類名(包名+類名),否則無法刪除快捷方式       
88.         String appClass = this.getPackageName() + "." +this.getLocalClassName();      
89.         ComponentName comp = new ComponentName(this.getPackageName(), appClass);      
90.         shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(Intent.ACTION_MAIN).setComponent(comp));      
91.         //發送卸載快捷方式的圖標           
92.         sendBroadcast(shortcut);      
93.                   
94.     }    
95. } 
 
註意:必須要在manifest.xml中聲明權限

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

摘自 BossDarcy的專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *