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

ANDROID 桌面快捷方式创建,和判断 返回false问题

2014-06-06 22:24 417 查看
创建快捷方式

/**
* 创建快捷方式
*/
public static void addShortcut(Activity context,Class calss)
{
if(isAddShortCut(context))
return ;
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
// 设置属性
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,context. getResources().getString(R.string.app_name));
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context.getApplicationContext(), R.drawable.ic_launcher);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);

// 是否允许重复创建
shortcut.putExtra("duplicate", false);

//设置桌面快捷方式的图标
Parcelable icon = Intent.ShortcutIconResource.fromContext(context,R.drawable.ic_launcher);
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(context,calss);

// 设置启动程序
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);

//广播通知桌面去创建
context.sendBroadcast(shortcut);
}


一般要判断下是否已经创建了

public static boolean isAddShortCut(Activity context) {
String url = "";
try {
url = "content://" + getAuthorityFromPermission(context, "com.android.launcher.permission.READ_SETTINGS")            + "/favorites?notify=true";
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(Uri.parse(url), new String[] { "title", "iconResource" }, "title=?", new String[] { context.getString(R.string.app_name).trim() }, null);
if (cursor != null && cursor.moveToFirst()) {
cursor.close();
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}


因为不同的手机厂商可能对手机系统进行了修改使用原生的

“content://com.android.launcher.settings/favorites?notify=true"

或者

"content://com.android.launcher2.settings/favorites?notify=true"

并不能准确判断 需要通过权限去获取当前手机provider.authority

/**
* 通过权限去获取当前手机provider.authority
* @param context
* @param permission
* @return
*/
public static String getAuthorityFromPermission(Context context, String permission) {
if (permission == null)
return null;
List<PackageInfo> packs = context.getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS);
if (packs != null) {
for (PackageInfo pack : packs) {
ProviderInfo[] providers = pack.providers;
if (providers != null) {
for (ProviderInfo provider : providers) {
if (permission.equals(provider.readPermission))
return provider.authority;
if (permission.equals(provider.writePermission))
return provider.authority;
}
}
}
}
return null;
}


权限配置,要在创建快捷方式的activity加 <action android:name="android.intent.action.CREATE_SHORTCUT"></action>

<activity
android:name="com.hw.weather.fuzhou.LaunchActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<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>


<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<!-- 创建删除快捷方式 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
<!-- 创建删除快捷方式  MOTO的部分机型还要加上以下权限 -->
<uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
<uses-permission android:name="com.android.launcher.permission.WRITE_SETTINGS" />
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: