Android Shortcuts 为其他应用创建桌面快捷方式,替换图标等

Android Shortcuts 为其他应用创建桌面快捷方式,替换图标等

shortcuts功能自android N_MR1(7.1,api 25)引入,使用ShortcutManager管理,
关于创建桌面图标pinned shortcuts是android O(8.0 api 26添加的,但是可以通过support包在低版本兼容)

  • 在查看官方文档时发现有这么一段话

    Note: See also the support library APIs, isRequestPinShortcutSupported() and requestPinShortcut(), which work on Android 7.1 (API level 25) and lower. The support library falls back to the deprecated EXTRA_SHORTCUT_INTENT extra to attempt the pinning process.

    • 创建桌面快捷图标在安卓7.1之前可以通过support(android.support.v4.content.pm)包兼容

创建

直接看代码

     class ShortcutIconBean{
             @JsonName("name")
             private String name;
             @JsonName("packageName")
             private String packageName;
             @JsonName("iconPath")
             private String iconPath;
          }
          
   class  xxx{
  
           /**添加快捷方式
              *@param  shortcutInfoIntent 创建桌面图标以后点击的执行逻辑
              *
              * */
         private void addShortcut(Intent shortcutInfoIntent,ShortcutIconBean shortCut) {
         Bitmap bitmap = BitmapUtils.decodeFile(shortCut.getIconPath());
         if (bitmap == null){
             Log.i(TAG,"bitmap == null");
             return;
         }
         String id = mThemeId + "-" + shortCut.getName() + "-" + System.currentTimeMillis();
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
             Log.i(TAG," Android O");
             ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
             ShortcutInfo  info = new ShortcutInfo.Builder(this, id)
                     .setIcon(Icon.createWithBitmap(bitmap))
                     .setShortLabel(shortCut.getName())
                     .setIntent(shortcutInfoIntent)
                     .build();
             Intent successCallback = new Intent(ACTION);
     /*    //In your app's manifest file, add ACTION_CREATE_SHORTCUT to the activity's <intent-filter> element.
           // 使用这个方式获取intent需要自己创建一个ACTIVITY管理创建成功回调,在ACTIVITY声明中加 ACTION_CREATE_SHORTCUT 过滤
      Intent successCallback =
                     shortcutManager.createShortcutResultIntent(info);*/
             PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(this, 200,
                     successCallback, PendingIntent.FLAG_UPDATE_CURRENT);
 
             shortcutManager.requestPinShortcut( info, shortcutCallbackIntent.getIntentSender());
 
         } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
             ShortcutInfoCompat info = new ShortcutInfoCompat.Builder(this, id)
                     .setIcon(IconCompat.createWithBitmap(bitmap))
                     .setShortLabel(shortCut.getName())
                     .setIntent(shortcutInfoIntent)
                     .build();
             Intent successCallback = new Intent(ACTION);
             //ACTION 处理创建结果的广播 
             PendingIntent shortcutCallbackIntent = PendingIntent.getBroadcast(this, 200,
                     successCallback, PendingIntent.FLAG_UPDATE_CURRENT);
 
             ShortcutManagerCompat.requestPinShortcut(this, info, shortcutCallbackIntent.getIntentSender());
//           注释部分为发广播的实现方式,不在使用
 /*            Log.i(TAG," Android N");
             Intent launcherIntent = new Intent(Intent.ACTION_MAIN);
             launcherIntent.setClass( this, JumpActivity.class);
             launcherIntent.addCategory(Intent.CATEGORY_LAUNCHER);
             launcherIntent.putExtra("packageName",shortCut.getPackageName());
           
 //            Intent launcherIntent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
             Intent installer = new Intent();
             //false标示不重复创建
             installer.putExtra("duplicate", false);
             installer.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launcherIntent);
             //设置应用的名称
             installer.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortCut.getName());
             //设置图标
             Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 128, 128, true);
             installer.putExtra(Intent.EXTRA_SHORTCUT_ICON,bitmap);
             installer.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
             //发送安装桌面图标的通知
             sendBroadcast(installer);
             Toast.makeText(this,"制作完成,请到桌面查看",Toast.LENGTH_SHORT).show();*/
         }
 
     }  
 }
          

版本判断然后执行不同代码,其实不同之处仅仅是将 ShortcutInfo 使用 ShortcutInfoCompat进行,

关于动态添加删除以及长按图标出现二级网上说得比较多

可参考shortcuts

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 在 Node.js 中创建桌面快捷方式的方法有以下几种: 1. 使用 `node-shortcut` 库: 可以使用 `node-shortcut` 库来创建桌面快捷方式。安装方法: ``` npm install node-shortcut ``` 然后,可以使用以下代码来创建快捷方式: ``` const shortcut = require('node-shortcut'); shortcut.create('C:\\path\\to\\your\\application.exe', 'My Shortcut', { target: 'C:\\path\\to\\your\\application.exe', cwd: 'C:\\path\\to\\your', icon: 'C:\\path\\to\\your\\icon.ico', description: 'My application shortcut', workingDir: 'C:\\path\\to\\your' }, function(err) { if (err) { console.error(err); } else { console.log('Shortcut created successfully'); } }); ``` 2. 使用 `child_process` 模块的 `exec` 方法: 可以使用 Node.js 的 `child_process` 模块的 `exec` 方法来执行系统命令,从而创建快捷方式。安装方法: ``` npm install child_process ``` 然后,可以使用以下代码来创建快捷方式: ``` const childProcess = require('child_process'); const targetPath = 'C:\\path\\to\\your\\application.exe'; const shortcutPath = 'C:\\path\\to\\your\\shortcut.lnk'; const iconPath = 'C:\\path\\to\\your\\icon.ico'; const description = 'My application shortcut'; const command = `powershell -Command "$WshShell = New-Object -ComObject WScript.Shell; $Shortcut = $WshShell.CreateShortcut('${shortcutPath}'); $Shortcut.TargetPath = '${targetPath}'; $Shortcut.IconLocation = '${iconPath}'; $Shortcut.Description = '${description}'; $Shortcut.Save();"`; childProcess.exec(command, function(err, stdout, stderr) { if (err) { console.error(err); } else { console.log('Shortcut created successfully'); } }); ``` 3. 使用 `node-windows` 库: 可以使用 `node-windows ### 回答2: 在Node.js中创建桌面快捷方式可以通过使用`windows-shortcuts`模块实现。以下是使用该模块在Windows操作系统中创建桌面快捷方式的步骤: 首先,通过以下命令安装`windows-shortcuts`模块: ```bash npm install windows-shortcuts ``` 接下来,在Node.js文件中导入`windows-shortcuts`模块: ```javascript const createShortcut = require('windows-shortcuts').create; ``` 然后,创建一个新的快捷方式对象,指定目标文件路径、快捷方式名称和输出路径: ```javascript const shortcut = createShortcut({ target: 'C:\\path\\to\\your\\file.exe', name: 'My Shortcut', output: 'C:\\Users\\Username\\Desktop' // 保存到桌面 }); ``` 在此示例中,`target`属性指定了所需创建快捷方式的文件路径,`name`属性指定了快捷方式的名称(你可以根据需要自定义),`output`属性指定了快捷方式的输出路径。 最后,使用`shortcut.save()`方法保存快捷方式文件: ```javascript shortcut.save(); ``` 此方法将在指定的输出路径中创建一个快捷方式文件。 完整的创建桌面快捷方式的示例代码如下: ```javascript const createShortcut = require('windows-shortcuts').create; const shortcut = createShortcut({ target: 'C:\\path\\to\\your\\file.exe', name: 'My Shortcut', output: 'C:\\Users\\Username\\Desktop' // 保存到桌面 }); shortcut.save(); ``` 请注意,此方法仅适用于Windows操作系统。如果你的应用程序需要在其他操作系统上创建桌面快捷方式,你需要使用相应的模块或方法来实现。 ### 回答3: 在Node.js中,可以使用第三方模块`node-os-utils`来创建桌面快捷方式。以下是基本的步骤: 1. 首先,确保已在您的项目中安装了`node-os-utils`模块。您可以使用以下命令来安装该模块: ``` npm install node-os-utils ``` 2. 在您的Node.js文件中,引入所需的模块: ```javascript const { Shortcut } = require('node-os-utils'); ``` 3. 使用`Shortcut`类创建一个`shortcut`对象: ```javascript const shortcut = new Shortcut(); ``` 4. 使用`create`方法创建一个桌面快捷方式。您需要提供快捷方式的目标路径、名称和图标路径(可选): ```javascript const targetPath = '/path/to/your/application.exe'; const shortcutName = 'My Application'; const iconPath = '/path/to/your/application.ico'; shortcut.create(targetPath, shortcutName, iconPath) .then(() => { console.log('桌面快捷方式创建'); }) .catch((error) => { console.error('创建桌面快捷方式时出错:', error); }); ``` 5. 运行上述Node.js文件,如果一切顺利,您将在桌面上看到一个名为`My Application`的快捷方式。 请注意,以上步骤假设您的操作系统是Windows。如果您使用的是其他操作系统,可能需要使用其他模块或方法来创建桌面快捷方式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林深人不知

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值