Android中程序向桌面和Launcher添加快捷方式

   最近感觉这个添加快捷方式挺有趣的,就查资料自己写了个demo---简单的例子,这个例子就是有两个按钮,点击“将此程序添加到快捷方式”,则手机桌面增加一个快捷方式,同时launcher中也多了一个快捷方式,点击退出,则提示:toast弹提示信息“退出程序”。知识梳理:Android平台上添加快捷方式有两种:一种桌面的快捷方式,一种是launcher的快捷方式。原理:是通过intent封装一些信息,以Broadcast的形式通知launcher创建快捷方式的!一定不要忘记在manifest.xml中注册一下权限

<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT">

在manifest.xml中加入一个动作过滤的intentFilter,快捷方式的列表中会多个该程序的快捷方式。

有问题或向说点什么的可以留言,欢迎大家批评和指正,转载请标明出处:

下面看一下程序的截图:  

                             程序的开始界面:                                     点击“将此程序添加快捷方式”按钮:

                                                     

         点击退出按钮,桌面多了快捷方式,弹Toast:            点出选择快捷方式后多了程序的快捷方式:

                                                       

在IntentWidget工程中:

一、在com.cn.daming包中IntentWidgetMainActivity.java中的代码:

[java]  view plain copy print ?
  1. <span><span>   
  2. </span></span><span style="font-size:13px;color:#000000;">package com.cn.daming;  
  3.   
  4. import android.app.Activity;  
  5. import android.content.Intent;  
  6. import android.os.Bundle;  
  7. import android.os.Parcelable;  
  8. import android.view.View;  
  9. import android.view.View.OnClickListener;  
  10. import android.widget.Button;  
  11. import android.widget.Toast;  
  12.   
  13. public class IntentWidgetMainActivity extends Activity implements OnClickListener{  
  14.   
  15.     private Button mStartWidgetButton;  
  16.     private Button mExitButton;  
  17.       
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         mStartWidgetButton = (Button) findViewById(R.id.my_button_1);  
  23.         mExitButton = (Button) findViewById(R.id.my_button_2);  
  24.         mStartWidgetButton.setOnClickListener(this);  
  25.         mExitButton.setOnClickListener(this);  
  26.     }  
  27.       
  28.     public void onClick(View v)  
  29.     {  
  30.         if(v == mStartWidgetButton){  
  31.             //inint the widgetIntent is declear  
  32.             Intent addWidgetIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
  33.                      <span style="white-space: pre-wrap;"></span>//whether repeat create is or not   
  34.                      addWdgetIntent.putExtra("duplicate",true);<span style="font-family: monospace;font-size:10px;"><span style="white-space: pre-wrap;"><span><span>  
  35. </span></span></span></span>  
  36.             //set the Widget of the title  
  37.             String mTitle = getResources().getString(R.string.my_title);  
  38.             //set the Widget of the icon  
  39.             Parcelable icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.widget_image);  
  40.               
  41.             Intent mIntent = new Intent(this,IntentWidgetMainActivity.class);  
  42.             addWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, mTitle);//set the title  
  43.             addWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);//set the icon  
  44.             addWidgetIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mIntent);//set the intent  
  45.             sendBroadcast(addWidgetIntent);  
  46.         }  
  47.         else if(v == mExitButton){  
  48.             finish();  
  49.             Toast.makeText(IntentWidgetMainActivity.this, R.string.exit, Toast.LENGTH_SHORT).show();  
  50.         }  
  51.     }  
  52. }</span>  

二、在layout目录下的main.xml中的代码:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:background="#00ffffff"  
  7.     >  
  8.     <TextView    
  9.         android:layout_width="fill_parent"   
  10.         android:layout_height="wrap_content"   
  11.         android:layout_marginTop="15dip"  
  12.         android:layout_marginBottom="15dip"  
  13.         android:gravity="center"  
  14.         android:text="@string/hello"  
  15.         android:textSize="8pt"  
  16.         />  
  17.     <Button  
  18.         android:id="@+id/my_button_1"  
  19.         android:layout_width="fill_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_marginBottom="10dip"  
  22.         android:textSize="10pt"  
  23.         android:text="@string/my_button_1"  
  24.     />  
  25.     <Button  
  26.         android:id="@+id/my_button_2"  
  27.         android:layout_width="fill_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_marginBottom="10dip"  
  30.         android:textSize="10pt"  
  31.         android:text="@string/my_button_2"  
  32.     />  
  33.     <TextView    
  34.         android:layout_width="fill_parent"   
  35.         android:layout_height="wrap_content"   
  36.         android:layout_marginBottom="15dip"  
  37.         android:gravity="center"  
  38.         android:text="@string/blogs"  
  39.         android:textSize="8pt"  
  40.         />  
  41. </LinearLayout>  

三、在values下的string.xml中的代码:

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">这是大明添加到Launcher的快捷方式</string>  
  4.     <string name="app_name">大明快捷方式!</string>  
  5.     <string name="my_button_1">将此程序添加快捷方式</string>  
  6.     <string name="my_button_2">退出程序</string>  
  7.     <string name="my_title">大明程序</string>  
  8.     <string name="exit">程序正在退出。。。。。。</string>  
  9.     <string name="blogs">博客地址:\n http://blog.csdn.net/wdaming1986/article/details/6877154</string>  
  10. </resources>  

 

四、manifest.xml 中的代码

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.cn.daming"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".IntentWidgetMainActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.             <!-- add the launch of my programmer`s quick launcher-->  
  16.             <intent-filter>  
  17.                 <action android:name="android.intent.action.CREATE_SHORTCUT"/>  
  18.             </intent-filter>  
  19.         </activity>  
  20.     </application>  
  21.     <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>  
  22. </manifest>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值