├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── bolex │ │ └── addshortcut │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── bolex │ │ │ └── addshortcut │ │ │ ├── MainActivity.java │ │ │ └── ShortCutUtils.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── bolex │ └── addshortcut │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AddShortcut 2 | 添加桌面快捷方式 3 | 4 | **配置权限** 5 | ```xml 6 |     7 | 8 | 9 | ``` 10 | **通过广播添加带有Intent的图标桌面** 11 | ```java 12 | public class ShortCutUtils { 13 | /** 14 | * 添加当活动为启动项 15 | * @param cx 16 | * @param name 快捷方式名称 17 | */ 18 | public static void addShortcut(Activity cx, String name) { 19 | // TODO: 2017/6/25 创建快捷方式的intent广播 20 | Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 21 | // TODO: 2017/6/25 添加快捷名称 22 | shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name); 23 | // 快捷图标是允许重复 24 | shortcut.putExtra("duplicate", false); 25 | // 快捷图标 26 | Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(cx, R.mipmap.ic_launcher); 27 | shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); 28 | // TODO: 2017/6/25 我们下次启动要用的Intent信息 29 | Intent carryIntent = new Intent(Intent.ACTION_MAIN); 30 | carryIntent.putExtra("name", name); 31 | carryIntent.setClassName(cx.getPackageName(),cx.getClass().getName()); 32 | carryIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 33 | //添加携带的Intent 34 | shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, carryIntent); 35 | // TODO: 2017/6/25 发送广播 36 | cx.sendBroadcast(shortcut); 37 | } 38 | 39 | } 40 | ``` 41 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.bolex.addshortcut" 8 | minSdkVersion 14 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.1.1' 28 | testCompile 'junit:junit:4.12' 29 | } 30 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\Android_sdk\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/bolex/addshortcut/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.bolex.addshortcut; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.bolex.addshortcut", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/bolex/addshortcut/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.bolex.addshortcut; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | import android.widget.Button; 7 | import android.widget.EditText; 8 | 9 | public class MainActivity extends AppCompatActivity { 10 | 11 | @Override 12 | protected void onCreate(Bundle savedInstanceState) { 13 | super.onCreate(savedInstanceState); 14 | setContentView(R.layout.activity_main); 15 | Button add = (Button) findViewById(R.id.add); 16 | final EditText name = (EditText) findViewById(R.id.name); 17 | add.setOnClickListener(new View.OnClickListener() { 18 | @Override 19 | public void onClick(View v) { 20 | ShortCutUtils.addShortcut(MainActivity.this, name.getText() != null ? name.getText().toString() : "1"); 21 | } 22 | }); 23 | } 24 | 25 | 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/java/com/bolex/addshortcut/ShortCutUtils.java: -------------------------------------------------------------------------------- 1 | package com.bolex.addshortcut; 2 | 3 | import android.app.Activity; 4 | import android.content.Intent; 5 | 6 | /** 7 | * 快捷方式添加工具类 8 | */ 9 | public class ShortCutUtils { 10 | /** 11 | * 添加当活动为启动项 12 | * @param cx 13 | * @param name 快捷方式名称 14 | */ 15 | public static void addShortcut(Activity cx, String name) { 16 | // TODO: 2017/6/25 创建快捷方式的intent广播 17 | Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); 18 | // TODO: 2017/6/25 添加快捷名称 19 | shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, name); 20 | // 快捷图标是允许重复 21 | shortcut.putExtra("duplicate", false); 22 | // 快捷图标 23 | Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(cx, R.mipmap.ic_launcher); 24 | shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes); 25 | // TODO: 2017/6/25 我们下次启动要用的Intent信息 26 | Intent carryIntent = new Intent(Intent.ACTION_MAIN); 27 | carryIntent.putExtra("name", name); 28 | carryIntent.setClassName(cx.getPackageName(),cx.getClass().getName()); 29 | carryIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 30 | //添加携带的Intent 31 | shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, carryIntent); 32 | // TODO: 2017/6/25 发送广播 33 | cx.sendBroadcast(shortcut); 34 | } 35 | 36 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 13 |