package com.shortcut;
import android.app.activity;
import android.content.contentresolver;
import android.content.intent;
import android.content.intent.shortcuticonresource;
import android.database.cursor;
import android.net.uri;
import android.os.bundle;
public class addshortcutactivity extends activity {
@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.ic_launcher);
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);
if (c != null && c.getcount() > 0) {
isinstallshortcut = true;
}
return isinstallshortcut;
}
}
xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shortcut"
android:versioncode="1"
android:versionname="1.0" >
<uses-sdk android:minsdkversion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".addshortcutactivity" >
<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 shortcut操作(一)
android shortcut操作(二)