监听local创建/删除桌面快捷方式的代码如下
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Log.e("MyReceiver", "---------onReceive------改语言------");
delShortcut(context);
addShortcut(context);
}
private void addShortcut(Context context) {
Intent mIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
MyApp.del = context.getString(R.string.app_name);
// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));
shortcut.putExtra("duplicate", true); // 不允许重复创建
// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
// 注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序
ComponentName comp = new ComponentName(context.getPackageName(), "." + "TestLanguageActivity");
Intent mintent= new Intent(Intent.ACTION_MAIN);
mintent.addCategory(Intent.CATEGORY_LAUNCHER);
mintent.setComponent(comp);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,mintent);
// 快捷方式的图标
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
context.sendBroadcast(shortcut);
}
private void delShortcut(Context context) {
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
// 快捷方式的名称
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, MyApp.del);
// shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));
// 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
// 注意: ComponentName的第二个参数必须是完整的类名(包名+类名),否则无法删除快捷方式
String appClass = context.getPackageName() + "." + "TestLanguageActivity";
ComponentName comp = new ComponentName(context.getPackageName(), appClass);
Intent mintent= new Intent(Intent.ACTION_MAIN);
mintent.addCategory(Intent.CATEGORY_LAUNCHER);
mintent.setComponent(comp);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, mintent);
context.sendBroadcast(shortcut);
}
}
你这是几个问题
我回答你的第四个问题吧就,希望对你有所帮助。
一般来说在 Android 中添加快捷方式的有以下两种:
在launcher的应用程序列表上,长按某一应用程序图标创建快捷方式到桌面
在桌面上长按在弹出框中选择快捷方式->应用程序->将添加快捷方式的程序
那么能不能在应用安装时自动将应用的快捷入口添加到桌面呢? 本文给大家分享一下相关的经验?
桌面是由launcher来控制的,所以我们可以通过下面两种方式来实现快捷方式的自动创建:
通过向launcher发送Broadcast让launcher创建快捷方式
为应用程序的组件注册某一个符合特定条件的IntentFilter,然后可以直接在Launcher的桌面添加启动该组件的快捷方式。
第一种方式:
/**
* 添加快捷方式到桌面 要点:
* 1.给Intent指定action="com.android.launcher.INSTALL_SHORTCUT"
* 2.给定义为Intent.EXTRA_SHORTCUT_INENT的Intent设置与安装时一致的action(必须要有)
* 3.添加权限:com.android.launcher.permission.INSTALL_SHORTCUT
*/
private void addShortcutToDesktop() {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 不允许重建
shortcut.putExtra("duplicate", false);
// 设置名字
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,this.getString(R.string.app_name));
// 设置图标
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this,
R.drawable.ic_launcher));
// 设置意图和快捷方式关联程序
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,new Intent(this, this.getClass()).setAction(Intent.ACTION_MAIN));
// 发送广播
sendBroadcast(shortcut);
}
当快捷方式创建成功后,launcher将通过toast的方式提示快捷方式创建成功,其中通过
shortCutIntent.putExtra("duplicate", false);设置不能重复创建,如果快捷方式已经创建则提示快捷方式已经创建
注意如果要让上述代码能成功运行,我们还需要设置Uses permission
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
第二种方式和第一种有些类似,不过我们不用广播的方式让给launcher创建,而是通过注册IntentFilter,由于“添加快捷方式”Action是 由Launcher通过startActivity-ForResult这一方法发出的,在Activity启动后把初始化的快捷方式 Intent返回给Launcher应用程序,设置结果值为RESULT_OK表示正常返回。
主要代码如下:
首先在xml中设置IntentFilter
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
复制代码创建核心代码:
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)) {
Intent shortcut = new Intent(Intent.ACTION_CREATE_SHORTCUT);
// 不允许重建
shortcut.putExtra("duplicate", false);
// 设置名字
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
this.getString(R.string.app_name));
// 设置图标
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this,
R.drawable.ic_launcher));
// 设置意图和快捷方式关联的程序
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,
new Intent(this, this.getClass()));
//将结果返回到launcher
setResult(RESULT_OK, intent);
}
在launcher中我们运行程序就可以将快捷方式创建在桌面上。
通过上述方式可以自动将快捷方式创建到桌面上,但是每次运行程序时都会将快捷方式创建到桌面上,下面我们将通过程序判断快捷方式是否已经创建到桌面上了,基本思路是:由于快捷方式launcher管理的,我们可以通过查看launcher中是否已经有此快捷方式数据,如果有就不在创建。
主要代码如下:
/**
* 添加权限:<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
*
* @return
*/
private boolean hasInstallShortcut() {
boolean hasInstall = false;
final String AUTHORITY = "com.android.launcher.settings";
Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/favorites?notify=true");
Cursor cursor = this.getContentResolver().query(CONTENT_URI,
new String[] { "title", "iconResource" }, "title=?",
new String[] { this.getString(R.string.app_name) }, null);
if (cursor != null && cursor.getCount() > 0) {
hasInstall = true;
}
return hasInstall;
}
上述查询操作,需要具有以下权限:
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"></uses-permission>
注意通过程序创建的快捷方式不会随着程序卸载而自动删除。
实现长按应用创建快捷方式:
代码:
Intent shortcutIntent = new Intent();
shortcutIntent.setClassName("packageName", "className");
//shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "shortcut_name");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(context, R.drawable.icon));
//intent.putExtra("duplicate", false);
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
context.sendBroadcast(addIntent);
然后再在AndroidManaifest.xml文件中设置:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
代码中注释里的根据需要选择