在程序第一次开启时自动在桌面添加一个快捷方式 我做了一个小的一键锁屏的程序,但是不知道怎么才能实现第一次开启时就在桌面自动建立一个快捷方式,不通过activity来创建快捷方式,就只是第一次打开程序后,自动在桌面建立一个快捷方式。感谢各位大神解救! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 给你一个工具类,直接在activity里调用一下就可以了.注意app权限<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"/>package com.jvmer.project.android.util;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.database.Cursor;import android.net.Uri;import android.os.Build;import android.util.Log;import com.jvmer.project.android.R;/** * 快捷方式工具类 * @author [email protected] * @page http://jvmer.com */public class ShortcutUtil { private static final String TAG = "ShortcutUtil"; /** * 检查并创建快捷方式 * @param activity 快捷方式指向的Activity */ public static void checkAndCreateShortCut(final Activity activity){ new Thread(){ public void run(){ if(!isShortCutExists(activity)){ addShortcut(activity); }else{ Log.d(TAG, "shortcut exists"); } } }.start(); } /** * 创建快捷方式 * @param activity * @return */ private static boolean addShortcut(Activity activity) { boolean result = false; try { Intent shortcut = new Intent(); shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, activity.getString(R.string.app_name)); shortcut.putExtra("duplicate", false); shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, getIntentShortcut(activity)); shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(activity, R.drawable.ic_launcher)); shortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); activity.sendOrderedBroadcast(shortcut,null); result = true; Log.d(TAG, "create shortcut success"); } catch (Exception e) { Log.e(TAG, "create shortcut exception", e); } return result; } /** * 快捷方式信息 * @param activity * @return */ private static Intent getIntentShortcut(Activity activity) { Intent i = new Intent(Intent.ACTION_MAIN); i.setComponent(activity.getComponentName()); i.addCategory(Intent.CATEGORY_LAUNCHER); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); return i; } /** * 判断快捷方式是否存在 * @param context * @return */ private static boolean isShortCutExists(Context context){ Cursor c = null; try { String AUTHORITY; if(getSystemVersion() > 8) { AUTHORITY = "com.android.launcher2.settings"; } else { AUTHORITY = "com.android.launcher.settings"; } Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true"); c = context.getContentResolver().query(CONTENT_URI, null, "title = ?", new String[]{context.getString(R.string.app_name)}, null); return (c != null && c.getCount() > 0); } finally { if(c != null) { c.close(); } } } /** * 判断系统版本 * @return */ private static int getSystemVersion() { return Build.VERSION.SDK_INT; }} 关于摇一摇 android --- google map 问题 怎么查看RadioButton是否被选中了 android中WebView如何设置代理 如何解析XML文件,并且将解析的内容布局到layout中 android 如何从短信监听服务中打开指定的应用 用Service怎么已进入程序就在后台播放音乐呢? 适配器的listview获取数据的问题。 请问Looper到底有什么用 自定义toast怎么全屏显示 android不下心把R文件删除了 急!Android图片保存的问题
注意app权限
<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"/>
package com.jvmer.project.android.util;import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.util.Log;import com.jvmer.project.android.R;/**
* 快捷方式工具类
* @author [email protected]
* @page http://jvmer.com
*/
public class ShortcutUtil {
private static final String TAG = "ShortcutUtil";
/**
* 检查并创建快捷方式
* @param activity 快捷方式指向的Activity
*/
public static void checkAndCreateShortCut(final Activity activity){
new Thread(){
public void run(){
if(!isShortCutExists(activity)){
addShortcut(activity);
}else{
Log.d(TAG, "shortcut exists");
}
}
}.start();
}
/**
* 创建快捷方式
* @param activity
* @return
*/
private static boolean addShortcut(Activity activity) {
boolean result = false;
try {
Intent shortcut = new Intent(); shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, activity.getString(R.string.app_name));
shortcut.putExtra("duplicate", false);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, getIntentShortcut(activity));
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(activity, R.drawable.ic_launcher));
shortcut.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
activity.sendOrderedBroadcast(shortcut,null);
result = true;
Log.d(TAG, "create shortcut success");
} catch (Exception e) {
Log.e(TAG, "create shortcut exception", e);
}
return result;
}
/**
* 快捷方式信息
* @param activity
* @return
*/
private static Intent getIntentShortcut(Activity activity) {
Intent i = new Intent(Intent.ACTION_MAIN);
i.setComponent(activity.getComponentName());
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
return i;
}
/**
* 判断快捷方式是否存在
* @param context
* @return
*/
private static boolean isShortCutExists(Context context){
Cursor c = null;
try {
String AUTHORITY;
if(getSystemVersion() > 8) {
AUTHORITY = "com.android.launcher2.settings";
} else {
AUTHORITY = "com.android.launcher.settings";
}
Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
c = context.getContentResolver().query(CONTENT_URI, null, "title = ?", new String[]{context.getString(R.string.app_name)}, null);
return (c != null && c.getCount() > 0);
} finally {
if(c != null) {
c.close();
}
}
}
/**
* 判断系统版本
* @return
*/
private static int getSystemVersion()
{
return Build.VERSION.SDK_INT;
}
}