Android桌面快捷方式的设置(二)
来源:互联网 发布:思科的网络基线 编辑:程序博客网 时间:2024/04/29 20:10
MainActivity如下:
package cn.testshortcut;import android.net.Uri;import android.os.Build;import android.os.Bundle;import android.app.Activity;import android.content.ContentResolver;import android.content.Context;import android.content.Intent;import android.content.pm.PackageManager;import android.content.pm.PackageManager.NameNotFoundException;import android.database.Cursor;/** * Demo描述: * 桌面快捷方式的设置 * * 备注说明: * 该方式只适合Android原生系统 * * 权限设置: * <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> * <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> */public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);addShortcutToDesktop(MainActivity.this);}public static void addShortcutToDesktop(Context context) {if (!isShortcutExist(context)) {Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");shortcutIntent.putExtra("duplicate", false);shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME,context.getString(R.string.app_name));shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context,R.drawable.ic_launcher));Intent intent = new Intent(context, MainActivity.class);intent.setAction("android.intent.action.MAIN");intent.addCategory("android.intent.category.LAUNCHER");shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);context.sendBroadcast(shortcutIntent);}}public static boolean isShortcutExist(Context context) {ContentResolver contentResolver = context.getContentResolver();String AUTHORITY = null;if (Build.VERSION.SDK_INT >= 8) {AUTHORITY = "com.android.launcher2.settings";} else {AUTHORITY = "com.android.launcher.settings";}String title = "";PackageManager packageManager = context.getPackageManager();try {title = packageManager.getApplicationLabel(packageManager.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA)).toString();} catch (NameNotFoundException e) {e.printStackTrace();}Uri uri = Uri.parse("content://" + AUTHORITY + "/favorites?notify=true");Cursor cursor = contentResolver.query(uri, null,"title=?", new String[] { title },null);if (cursor != null && cursor.getCount() > 0) {return true;}return false;}}
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="建立桌面快捷方式" android:layout_centerInParent="true" /></RelativeLayout>
- Android桌面快捷方式的设置(二)
- Android桌面快捷方式的设置(一)
- Android桌面快捷方式的实现
- Android桌面快捷方式的实现
- Android桌面快捷方式的实现
- Android桌面 快捷方式的删除
- Android桌面快捷方式的实现
- Android之生成桌面快捷方式(二)
- android 快捷方式开发(二)桌面添加快捷方式
- 如何给桌面的快捷方式设置快捷键
- Android桌面组件开发之桌面快捷方式(二)
- 设置桌面快捷方式
- 如何设置桌面快捷方式
- Android Launcher桌面应用快捷方式的开发
- Android Launcher桌面应用快捷方式的开发
- Android Launcher桌面应用快捷方式的开发
- Android Launcher-----桌面应用快捷方式的开发!
- Android创建桌面快捷方式的方法
- 多线程的优点
- Wireshark基本介绍和学习TCP三次握手
- JSF项目中实现基于RBAC模型的权限管理设计
- 无病呻吟,把自己看透了已经。。。
- 多线程的代价
- Android桌面快捷方式的设置(二)
- android Jni AttachCurrentThread 内存泄露
- 如何创建并运行java线程
- android getSharedPreferences有两个参数
- HDOJ2149[巴什博弈]输出走法
- Android编译系统详解
- 竞态条件与临界区
- Using WebSocket in .NET 4.5 (Part 3)
- 线程安全与共享资源