我想写一个程序按下按钮就往桌面创建一个快捷方式,这个快捷方式指向桌面的拨号程序,请问各位大虾该怎么做啊

解决方案 »

  1.   

    public class main extends Activity {
    /** Called when the activity is first created. */ @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    if (isS() == false) {
    addShortcut(this, "com.Shortcut");  //改成自己的包名
    }
    } /**
     * 检查桌面是否有app快捷方式   defy验证通过
     * @return
     */
    private boolean isS() {
    boolean isInstallShortcut = false;
    final ContentResolver cr = this.getContentResolver();
    final String 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[] { "Shortcut" },// XXX表示应用名称。
    null);
    if (c != null && c.getCount() > 0) {
    isInstallShortcut = true;
    }
    return isInstallShortcut;
    } /**
     * @param context    执行者。
     * @params pkg 待添加快捷方式的应用包名,其值不可为null。
     * @return 返回true为正常执行完毕,<br/>
     *   返回false为pkg值为null或者找不到该应用或者该应用无用于Launch的MainActivity 。
     * */ public boolean addShortcut(Context context, String pkg) { // 快捷方式名
    String title = "unknown";

    // MainActivity完整名
    String mainAct = null;

    // 应用图标标识
    int iconIdentifier = 0;

    // 根据包名寻找MainActivity
    PackageManager pkgMag = context.getPackageManager();
    Intent queryIntent = new Intent(Intent.ACTION_MAIN, null);
    queryIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    List<ResolveInfo> list = pkgMag.queryIntentActivities(queryIntent,
    PackageManager.GET_ACTIVITIES);
    for (int i = 0; i < list.size(); i++) {
    ResolveInfo info = list.get(i);
    if (info.activityInfo.packageName.equals(pkg)) {
    title = info.loadLabel(pkgMag).toString();
    mainAct = info.activityInfo.name;
    iconIdentifier = info.activityInfo.applicationInfo.icon;
    break;
    }
    }
    if (mainAct == null) {
    // 没有启动类
    return false;
    }
    Intent shortcut = new Intent(
    "com.android.launcher.action.INSTALL_SHORTCUT");
    // 快捷方式的名称
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
    // shortcut.putExtra("duplicate", false); //不允许重复创建
    // 指定当前的Activity为快捷方式启动的对象: 如 com.everest.video.VideoPlayer
    // 注意: ComponentName的第二个参数必须加上点号(.),否则快捷方式无法启动相应程序
    ComponentName comp = new ComponentName(pkg, mainAct);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(
    Intent.ACTION_MAIN).setComponent(comp));
    // 快捷方式的图标
    Context pkgContext = null;
    if (context.getPackageName().equals(pkg)) {
    pkgContext = context;
    } else {
    // 创建第三方应用的上下文环境,为的是能够根据该应用的图标标识符寻找到图标文件。
    try {
    pkgContext = context.createPackageContext(pkg,
    Context.CONTEXT_IGNORE_SECURITY
    | Context.CONTEXT_INCLUDE_CODE);
    } catch (NameNotFoundException e) {
    e.printStackTrace();
    }
    }
    if (pkgContext != null) {
    ShortcutIconResource iconRes = Intent.ShortcutIconResource
    .fromContext(pkgContext, iconIdentifier);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);
    }
    // 发送广播,让接收者创建快捷方式
    // 需权限<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
    context.sendBroadcast(shortcut);
    return true; }}