Android创建桌面快捷方式几种方法

来源:互联网 发布:编程机器人哪个更好 编辑:程序博客网 时间:2024/05/19 02:03
Java代码  收藏代码
  1. Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成。  
  2.   
  3. 第一个是通过广播(Broadcast)的形式向Luncher发送请求生成快捷方式的。在网上找到关于这方面的注册信息。  
  4.   
  5. <!--     
  6.     
  7. Code highlighting produced by Actipro CodeHighlighter (freeware)     
  8. http://www.CodeHighlighter.com/     
  9.     
  10. --><!--设置wallpapaer的activity -->    
  11.         <!-- Intent received used to install shortcuts from other applications -->            
  12.           
  13. <receiver                
  14.     android:name="com.android.launcher2.InstallShortcutReceiver"                
  15.     android:permission="com.android.launcher.permission.INSTALL_SHORTCUT">                
  16.     <intent-filter>                    
  17.         <action android:name="com.android.launcher.action.INSTALL_SHORTCUT" />                
  18.     </intent-filter>            
  19. </receiver>   
  20.   
  21.         可以看出,要在桌面上创建快捷方式就需要权限了:  
  22.   android:permission="com.android.launcher.permission.INSTALL_SHORTCUT。  
  23.   所以在我们的manifest.xml文件中,我们需要加入下面这段话:  
  24.         <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>    
  25.           
  26.         下面就是代码层的实现:  
  27.   假如我在一个activity中创建一个创建快捷方式的方法:createShortCut();  
  28.   
  29. public void createShortCut(){     
  30.     //创建快捷方式的Intent                     
  31.     Intent shortcutintent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");                     
  32.     //不允许重复创建                     
  33.     shortcutintent.putExtra("duplicate"false);                     
  34.     //需要现实的名称                     
  35.     shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));     
  36.     //快捷图片                    
  37.     Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);     
  38.     shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);     
  39.     //点击快捷图片,运行的程序主入口                     
  40.     shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , EnterActivity.class));                     
  41.     //发送广播。OK                     
  42.     sendBroadcast(shortcutintent);     
  43. }  
  44.   
  45.   
  46.   
  47. 二、长按桌面弹出的桌面快捷方式创建  
  48.   
  49.         如何在添加到一个SHORTCUTS列表中,就是你长按桌面弹出来的那个东东。  
  50.   首先在注册activity时,需要添加一个action为android.intent.action.CREATE_SHOERTCUT的intentFilter.如下所示:  
  51.       
  52.     <activity android:name="ShortCutTest">                
  53.         <intent-filter>                    
  54.             <action android:name="android.intent.action.CREATE_SHORTCUT"/>                
  55.         </intent-filter>            
  56.     </activity>   
  57.       
  58.     接下来就是就是设置快捷方式的图标、名称、事件等属性。这里图表的生成,android里提供了专门的方法来生成。  
  59.   
  60.   
  61. public class ShortCutTest extends Activity{         
  62.     @Override        
  63.     protected void onCreate(Bundle savedInstanceState) {             
  64.     // TODO Auto-generated method stub             
  65.     super.onCreate(savedInstanceState);         
  66.         createShortCut();  
  67.     }     
  68.          
  69.     public void createShortCut(){             
  70.         Intent addShortCut;     
  71.         //判断是否需要添加快捷方式             
  72.         if(getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)){                 
  73.             addShortCut = new Intent();                 
  74.             //快捷方式的名称                 
  75.             addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME , "我的快捷方式");                 
  76.             //显示的图片                
  77.             Parcelable icon = ShortcutIconResource.fromContext(this, R.drawable.icon);                 
  78.             addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);     
  79.             //快捷方式激活的activity,需要执行的intent,自己定义                 
  80.             addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent());                 
  81.             //OK,生成                 
  82.             setResult(RESULT_OK, addShortCut);     
  83.         }else{                 
  84.              //取消                 
  85.             setResult(RESULT_CANCELED);     
  86.         }     
  87.     }     
  88. }   


转载地址:http://1002878825-qq-com.iteye.com/blog/1828669


0 0
原创粉丝点击