Android生成快捷方式
package com.qust.icon;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import com.qust.icon.*;

public class MainActivity extends Activity
 {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}
	public void click1(View view) {
		Parcelable icon = Intent.ShortcutIconResource.fromContext(
				getApplicationContext(), R.drawable.logo);
		Intent intent = new Intent();
		intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
		//生成图标的名称
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "你能");
		//不允许重复创建
		//intent.putExtra("duplicate", false); 
		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
		Intent intent2 = new Intent();
		intent2.setAction(Intent.ACTION_MAIN);
		intent2.addCategory(Intent.CATEGORY_LAUNCHER);
		intent2.setComponent(new ComponentName(this, MainActivity.class));
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent2);
		sendBroadcast(intent);
	}}

布局文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="https://schemas.android.com/apk/res/android"
	xmlns:tools="https://schemas.android.com/tools"
	android:layout_width="match_parent"
	android:layout_height="match_parent"
	android:orientation="vertical">

	<Button
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:text="生成图标"
		android:onClick="click1"/>

</LinearLayout>

添加权限:

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

© 版权声明
THE END
喜欢就支持以下吧
点赞0赞赏
分享
评论 共2条

请登录后发表评论