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"/>

 版权声明:本文为AIDE教程网原创文章,转载请附上原文出处链接和本声明
本文链接: https://www.aidemx.cn/288.html

梦雪

专注于AIDE教程分享

2 Comments

  • 真快

    • @冰崖 百度不收录我网站,呜呜

留下你的评论

*评论支持代码高亮<pre class="prettyprint linenums">代码</pre>

相关推荐