Android添加桌面快捷方式
来源:互联网 发布:办公软件书籍 编辑:程序博客网 时间:2024/04/29 07:46
转载自:http://www.cnblogs.com/DswCnblog/archive/2012/09/16/2687729.html
获取系统版本的方法
public int getAndroidSDKVersion(){int version ;version = Integer.valueOf(android.os.Build.VERSION.SDK);return version;}
如何判断快捷方式是否已经创建的方法,因为快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中,所以可以查询此表得到
一般android应用程序安装完成后是不会自动创建快捷方式的,所以可以自己在程序启动时实现。
需要权限
和要点击快捷方式对应的那个activity的属性。
以下是代码片段:
<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>
如何判断快捷方式是否已经创建的方法,因为快捷方式信息是保存在com.android.launcher的launcher.db的favorites表中,所以可以查询此表得到,
需要权限
下面给个例子分享给各位:
以下是代码片段:
import
android.content.ContentResolver;
import
android.content.Intent;
import
android.content.Intent.ShortcutIconResource;
import
android.database.Cursor;
import
android.graphics.BitmapFactory;
import
android.net.Uri;
import
android.os.Bundle;
public
class
AddShortCutActivity
extends
Activity {
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
boolean
flag =IfaddShortCut();
//如果已经创建,则不需要在创建
if
(flag==
false
){
addShortCut();
}
}
public
void
addShortCut(){
Intent shortcut =
new
Intent(
"com.android.launcher.action.INSTALL_SHORTCUT"
);
// 设置属性
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, getResources().getString(R.string.app_name));
ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(AddShortCutActivity.
this
, R.drawable.icon);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,iconRes);
// 是否允许重复创建
shortcut.putExtra(
"duplicate"
,
false
);
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(AddShortCutActivity.
this
, AddShortCutActivity.
class
);
// 设置启动程序
System.out.println(
"createIcon"
);
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
AddShortCutActivity.
this
.sendBroadcast(shortcut);
}
public
boolean
IfaddShortCut(){
boolean
isInstallShortcut =
false
;
final
ContentResolver cr = AddShortCutActivity.
this
.getContentResolver();
//本人的2.2系统是”com.android.launcher2.settings”,网上见其他的为"com.android.launcher.settings"
final
String AUTHORITY =
"com.android.launcher2.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[] {getString(R.string.app_name ) },
null
);
//XXX表示应用名称。
if
(c!=
null
&& c.getCount()>
0
){
isInstallShortcut =
true
;
System.out.println(
"已创建"
);
}
return
isInstallShortcut ;
}
}
以下是代码片段:
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
package
=
"com.shao.add"
android:versionCode=
"1"
android:versionName=
"1.0"
>
<uses-sdk android:minSdkVersion=
"8"
/>
<application android:icon=
"@drawable/icon"
android:label=
"@string/app_name"
>
<activity android:name=
".AddShortCutActivity"
android:label=
"@string/app_name"
>
<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>
</application>
<uses-permission android:name=
"com.android.launcher.permission.READ_SETTINGS"
/>
<uses-permission android:name=
"com.android.launcher.permission.INSTALL_SHORTCUT"
/>
</manifest>
- android桌面添加快捷方式
- Android添加桌面快捷方式
- android 添加桌面快捷方式
- Android 向桌面添加快捷方式
- android添加删除桌面快捷方式
- 【Android】添加删除桌面快捷方式
- Android 向桌面添加快捷方式
- android添加删除桌面快捷方式
- Android 向桌面添加快捷方式
- Android 添加桌面快捷方式操作
- Android程序添加桌面快捷方式
- android应用添加桌面快捷方式
- android 添加快捷方式到桌面
- Android添加桌面快捷方式和删除桌面快捷方式
- Android 为程序添加桌面快捷方式
- 为android应用程序添加桌面快捷方式
- Android 为程序添加桌面快捷方式
- android自动添加程序桌面快捷方式
- Java进阶篇
- tar.xz文件如何解压
- 第三周—1.第一个面向对象的程序
- 实例讲解asp.net 接口的作用(好处)
- Linux学习笔记(二)
- Android添加桌面快捷方式
- js屏蔽鼠标右键菜单
- Dancing Links
- SUMO仿真快速入门系列四:Ubuntu 12.04 安装ns2.35
- 黑马程序员---java基础之Map
- Linux学习笔记(三)
- android display: lcd 线程角度看待刷屏的控制过程
- MFC篇:打开文件/文件夹
- Linux学习笔记(四)