现在的位置: 首页 > 综合 > 正文

Android 桌面快捷方式的创建与删除

2018年05月06日 ⁄ 综合 ⁄ 共 3789字 ⁄ 字号 评论关闭

Android桌面快捷方式的创建与删除,研究后记录一下

解决了以下问题:

1、  第一次安装后自动创建快捷方式

2、  点击创建如果已经创建则不重复创建

3、  卸载后桌面快捷方式也一起删除、

4、  重新安装后如果已经存在快捷方式则不重复创建

转载加地址http://blog.csdn.net/jing110fei/article/details/39078619

首先在AndroidManifest.xml中添加权限

<!-- 指定添加和删除安装快捷方式的权限 -->  
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />

 

布局文件activity_add_shortcut.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="@dimen/padding_medium"
        android:text="@string/hello_world"
        tools:context=".AddShortcutActivity" />
    <Button 
        android:id="@+id/creat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/creat"
        android:layout_gravity="center"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="@dimen/padding_medium"
        android:text="@string/delete_world"
        tools:context=".AddShortcutActivity" />
    <Button 
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/delete"
        android:layout_gravity="center"/>
</LinearLayout>

Strings.xml

<resources>

    <string name="app_name">测试应用</string>
    <string name="hello_world">点击按钮创建快捷方式</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_add_shortcut">AddShortcutActivity</string>
    <string name="creat">创建</string>
    <string name="delete">删除</string>
    <string name="delete_world">点击按钮删除快捷方式</string>
</resources>

 

主Activity AddShortcutActivity.java

public class AddShortcutActivity extends Activity implements OnClickListener {
	private Button button; 
	private Button button2; 
	String action; 
	String category;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_shortcut);
        action = "android.intent.action.MAIN";  
        category="android.intent.category.LAUNCHER";
        add();
        button=(Button)findViewById(R.id.creat);
        button.setOnClickListener(this);
        button2=(Button)findViewById(R.id.delete);
        button2.setOnClickListener(this);
    }
    
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.creat:
				add();
				break;
		case R.id.delete:
				delete();	
				break;
		default:
			break;
		}
	}
	public void add() {
		Intent addSC=new Intent("com.android.launcher.action.INSTALL_SHORTCUT");  
		//快捷键的标题   
		String title=getResources().getString(R.string.app_name);  
		//快捷键的图标   
		Parcelable icon=Intent.ShortcutIconResource.fromContext(  
		        AddShortcutActivity.this, R.drawable.ic_launcher);  
		//创建单击快捷键启动本程序的Intent   
		Intent launcherIntent=new Intent(AddShortcutActivity.this, AddShortcutActivity.class);  
		/**要使删除应用能够使用,这里必须为Intent设置一个action,可以任意(但安装和卸载时该参数必须一致)  
		 * 又要实现卸载应用的时候删除桌面快捷方式,就得这么定义,跟程序绑定
		 *  action = "android.intent.action.MAIN";  
		 *	category="android.intent.category.LAUNCHER";
		 * */
		launcherIntent.setAction(action);
		launcherIntent.addCategory(category); 

		//不允许重复创建快捷方式
		addSC.putExtra("duplicate", false);
		//设置快捷键的标题   
		addSC.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);  
		//设置快捷键的图标   
		addSC.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);  
		//设置单击此快捷键启动的程序   
		addSC.putExtra(Intent.EXTRA_SHORTCUT_INTENT,launcherIntent);  
		//向系统发送添加快捷键的广播   
		sendBroadcast(addSC);  
	}
	public void delete() {
		Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");  
	      // 快捷方式的名称  
	      shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));        
	      Intent respondIntent = new Intent(AddShortcutActivity.this, AddShortcutActivity.class);  
	      respondIntent.setAction(action); 
	      respondIntent.addCategory(category); 
	      shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, respondIntent);     
	      sendBroadcast(shortcut);  
		
	}
}

 

全部代码就在这里了,大家可以看一下,做个参考可怜

 

 

抱歉!评论已关闭.