当前位置:编程学习 > wap >>

在程序第一次开启时自动在桌面添加一个快捷方式

我做了一个小的一键锁屏的程序,但是不知道怎么才能实现第一次开启时就在桌面自动建立一个快捷方式,不通过activity来创建快捷方式,就只是第一次打开程序后,自动在桌面建立一个快捷方式。
感谢各位大神解救! --------------------编程问答-------------------- 给你一个工具类,直接在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 butnet@jvmer.com
 * @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
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,