您的位置:首页 > 移动开发

快捷方式:给APP内部某一个Activity创建快捷方式(类似QQ好友快捷方式)

2014-12-17 09:25 417 查看
第一步:添加权限:

<!-- 桌面快捷方式 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />


第二步:
为应用程序组件注册一个符合特定条件的IntentFilter,然后就可以直接在Launcher的桌面上添加启动该组件的快捷方式了。

当我们想把添加的快捷方式的Activity添加进这列时,只需要在这个Activity注册时添加一个Action为android.intent.action.CREATE_SHORTCUT的IntentFilter就可以。

若不注册一个符合特定条件的IntentFilter,创建快捷方式成功后,点击快捷方式时,则会提示,您的手机未安装应用程序。

<activity
android:name="com.qianyanshangwu.qianyanlife.BusinessListActivity"
android:screenOrientation="portrait"
android:theme="@style/QianyanTheme.Translucent.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
</intent-filter>
</activity>


第三步:创建快捷方式:

/**
* 创建指定Activity的快捷方式
*
* @param context
* @param cls
*            目标Activity
* @param bundle
*            传递给Activity的数据
* @param name
*            快捷方式标题
* @param resId
*            快捷方式图标
*/
public static void createShortCut(Context context, Class<?> cls, Bundle bundle, String name, int resId) {
Intent targetIntent = new Intent();
targetIntent.setClass(context, cls);
if (bundle != null) {
targetIntent.putExtras(bundle);
}

Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
Parcelable icon = Intent.ShortcutIconResource.fromContext(context, resId); // 获取快捷键的图标
shortcutIntent.putExtra("duplicate", false); // 设置快捷方式不能重复

shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);// 快捷方式的标题
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的图标
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);// 快捷方式的动作
context.sendBroadcast(shortcutIntent);
}


另,附快捷方式其他操作:

import java.util.HashMap;
import java.util.Map;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.DisplayMetrics;

import com.qianyanshangwu.qianyanlife.BusinessListActivity;
import com.qianyanshangwu.qianyanlife.R;
import com.qianyanshangwu.qianyanlife.SplashActivity;

/**
* APP信息
*
* @author zkw
*
*/
public class AppUtils {

/**
*
* 屏幕高
*
* @param context
* @return
*/
public static int getScreenWidth(Context context) {
DisplayMetrics dm = new DisplayMetrics();
dm = context.getResources().getDisplayMetrics();
return dm.widthPixels;
}

/**
* 获取当前应用程序的版本号
*
* @return
*/
public static String getVersion(Context context) {
try {
// 获取清单文件中当前程序的版本号
// 要想获取到清单文件,首先获取当前apk对应的包
PackageManager manager = context.getPackageManager();
// 第一个参数:包名,既可以直接写"cn.example.phonesafe",也可以通过getPackageName获取
// 第二个参数不关心,设置为0
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);// 获取包的信息
return info.versionName;
} catch (NameNotFoundException e) {
e.printStackTrace();
return "0";
}
}

/**
* 是否已创建快捷方式
*
* @param context
* @return
*/
public static boolean hasShortcut(Context context) {
boolean isInstallShortcut = false;
final String AUTHORITY = "com.android.launcher.settings";
final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");
Cursor c = context.getContentResolver().query(CONTENT_URI, new String[] { "title", "iconResource" }, "title=?", new String[] { context.getString(R.string.app_name).trim() }, null);
if (null != c && c.getCount() > 0) {
isInstallShortcut = true;
}
return isInstallShortcut;
}

/**
* 创建桌面快捷方式
*/
public static void createShortCutDialog(final Context context) {
Dialog alertDialog = new AlertDialog.Builder(context)//
.setTitle("温馨提示").setMessage("是否创建桌面快捷方式?").setIcon(R.drawable.ic_launcher)//
.setPositiveButton("创建", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
createShortCut(context);
}

})//
.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create();
alertDialog.show();
}

/**
* 创建快捷方式
*/

public static void createShortCut(Context context) {
Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
Parcelable icon = Intent.ShortcutIconResource.fromContext(context, R.drawable.ic_launcher); // 获取快捷键的图标
shortcutIntent.putExtra("duplicate", false); // 设置快捷方式不能重复

Intent targetIntent = new Intent(context, SplashActivity.class);
// 卸载程序后删除快捷方式
targetIntent.setAction("android.intent.action.MAIN");
targetIntent.addCategory("android.intent.category.LAUNCHER");

shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));// 快捷方式的标题
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的图标
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);// 快捷方式的动作
context.sendBroadcast(shortcutIntent);
PreferencesManager.putBoolean(context, "SHORTCUT", true);
}

/**
* 创建指定Activity的快捷方式
*
* @param context
* @param cls
*            目标Activity
* @param bundle
*            传递给Activity的数据
* @param name
*            快捷方式标题
* @param resId
*            快捷方式图标
*/
public static void createShortCut(Context context, Class<?> cls, Bundle bundle, String name, int resId) {
Intent targetIntent = new Intent();
targetIntent.setClass(context, cls);
if (bundle != null) {
targetIntent.putExtras(bundle);
}

Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
Parcelable icon = Intent.ShortcutIconResource.fromContext(context, resId); // 获取快捷键的图标
shortcutIntent.putExtra("duplicate", false); // 设置快捷方式不能重复

shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);// 快捷方式的标题
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);// 快捷方式的图标
shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);// 快捷方式的动作
context.sendBroadcast(shortcutIntent);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐