Android 创建桌面快捷方式
来源:互联网 发布:淘宝关键词设置 编辑:程序博客网 时间:2024/04/29 14:39
如何判断快捷方式是否已经创建的方法,因为快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中,所以可以查询此表得到
一般android应用程序安装完成后是不会自动创建快捷方式的,所以可以自己在程序启动时实现。
需要权限和要点击快捷方式对应的那个activity的属性。
以下是代码片段:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"></action>
</intent-filter>
如何判断快捷方式是否已经创建的方法,因为快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中,所以可以查询此表得到,
public boolean isAddShortCut() {
boolean isInstallShortcut = false;
final ContentResolver cr = this.getContentResolver();
int versionLevel = android.os.Build.VERSION.SDK_INT;
String AUTHORITY = "com.android.launcher2.settings";
//2.2以上的系统的文件文件名字是不一样的
if (versionLevel >= 8) {
AUTHORITY = "com.android.launcher2.settings";
} else {
AUTHORITY = "com.android.launcher.settings";
}
final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/favorites?notify=true");
Cursor c = cr.query(CONTENT_URI,
new String[] { "title", "iconResource" }, "title=?",
new String[] { getString(R.string.app_name) }, null);
if (c != null && c.getCount() > 0) {
isInstallShortcut = true;
}
return isInstallShortcut;
}
添加快捷方式:
public void addShortCut(){
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 设置属性
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this.getApplicationContext(), R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);
// 是否允许重复创建
shortcut.putExtra("duplicate", false);
//设置桌面快捷方式的图标
Parcelable icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
//点击快捷方式的操作
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(AddShortCutActivity.this, AddShortCutActivity.class);
// 设置启动程序
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
//广播通知桌面去创建
this.sendBroadcast(shortcut);
}
上面的判断是否添加快捷方式和添加快捷方式都是在activity的onCreate()方法中被调用的。
if(!isAddShortCut()){
addShortCut();
}
androidManifest.xml文件的片段
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shao.add"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AddShortCutActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"></action>
</intent-filter>
</activity>
</application>
<!-- 需要权限 -->
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
</manifest>
一般android应用程序安装完成后是不会自动创建快捷方式的,所以可以自己在程序启动时实现。
需要权限和要点击快捷方式对应的那个activity的属性。
以下是代码片段:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"></action>
</intent-filter>
如何判断快捷方式是否已经创建的方法,因为快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中,所以可以查询此表得到,
public boolean isAddShortCut() {
boolean isInstallShortcut = false;
final ContentResolver cr = this.getContentResolver();
int versionLevel = android.os.Build.VERSION.SDK_INT;
String AUTHORITY = "com.android.launcher2.settings";
//2.2以上的系统的文件文件名字是不一样的
if (versionLevel >= 8) {
AUTHORITY = "com.android.launcher2.settings";
} else {
AUTHORITY = "com.android.launcher.settings";
}
final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
+ "/favorites?notify=true");
Cursor c = cr.query(CONTENT_URI,
new String[] { "title", "iconResource" }, "title=?",
new String[] { getString(R.string.app_name) }, null);
if (c != null && c.getCount() > 0) {
isInstallShortcut = true;
}
return isInstallShortcut;
}
添加快捷方式:
public void addShortCut(){
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 设置属性
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(this.getApplicationContext(), R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);
// 是否允许重复创建
shortcut.putExtra("duplicate", false);
//设置桌面快捷方式的图标
Parcelable icon = Intent.ShortcutIconResource.fromContext(this,R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,icon);
//点击快捷方式的操作
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(AddShortCutActivity.this, AddShortCutActivity.class);
// 设置启动程序
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
//广播通知桌面去创建
this.sendBroadcast(shortcut);
}
上面的判断是否添加快捷方式和添加快捷方式都是在activity的onCreate()方法中被调用的。
if(!isAddShortCut()){
addShortCut();
}
androidManifest.xml文件的片段
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shao.add"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".AddShortCutActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT"></action>
</intent-filter>
</activity>
</application>
<!-- 需要权限 -->
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS"/>
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
</manifest>
0 0
- android 创建桌面快捷方式
- Android 创建桌面快捷方式
- android创建桌面快捷方式
- Android 创建桌面快捷方式
- android 创建桌面快捷方式
- Android 创建桌面快捷方式
- Android创建桌面快捷方式
- android 创建桌面快捷方式
- Android 创建桌面快捷方式
- Android 创建桌面快捷方式
- Android 创建桌面快捷方式
- Android:桌面创建快捷方式
- Android创建桌面快捷方式
- android 创建桌面快捷方式
- Android创建桌面快捷方式
- android 创建桌面快捷方式
- Android创建桌面快捷方式
- Android创建桌面快捷方式
- Android复习练习一(简单拨号器)
- 对象间的联动——观察者模式
- 动态规划2:最长非升子序列和最长非降子序列
- DSP6000的几个简单优化技巧
- Android多线程学习(一):使用Thread异步下载图像
- Android 创建桌面快捷方式
- Binary Tree Zigzag Level Order Traversal 二叉树锯齿形层次遍历
- 数字信号处理的学习资源
- 关于typedef的用法总结
- 用 Java 生成 Java - CodeModel 介绍
- 用树莓派搭建wordpress博客
- 分隔符
- 风该军阀割据该
- 白话压缩感知(含Matlab代码)