Android之ShortCut两种方式整合[支持长按创建快捷方式和应用第一次启动时创建快捷方式]
ShortCutActivity
package
com.zhy.shortcut;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ShortCutActivity extends Activity {
private static final String CREATE_SHORTCUT_ACTION = " com.android.launcher.action.INSTALL_SHORTCUT " ;
private static final String DROP_SHORTCUT_ACTION = " com.android.launcher.action.UNINSTALL_SHORTCUT " ;
private static final String PREFERENCE_KEY_SHORTCUT_EXISTS = " IsShortCutExists " ;
Button button;
// 获取默认的SharedPreferences
SharedPreferences sharedPreferences ;
// 从SharedPreferences获取是否存在快捷方式 若不存在返回false 程序第一次进来肯定返回false
boolean exists ;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences( this );
exists = sharedPreferences.getBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS, false );
// 创建桌面快捷方式
// 若第一次启动则创建,下次启动则不创建
if ( ! exists) {
setUpShortCut();
}
Intent intent = getIntent();
String action = intent.getAction();
// 长按桌面 创建快捷方式
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)){
Intent shortCut = new Intent(Intent.ACTION_MAIN);
shortCut.setClassName( this , this .getClass().getName());
Intent data = new Intent();
data.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext( this , R.drawable.logo));
data.putExtra(Intent.EXTRA_SHORTCUT_NAME, " sina " );
data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCut);
data.putExtra( " duplicate " , false );
setResult(RESULT_OK, data);
finish();
return ;
}
setContentView(R.layout.main);
button = (Button) findViewById(R.id.dropShortCut);
button.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
tearDownShortCut();
}
});
}
/**
* 启动时创建桌面快捷方式
*/
private void setUpShortCut() {
Intent intent = new Intent(CREATE_SHORTCUT_ACTION);
// 设置快捷方式图片
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext( this , R.drawable.logo));
// 设置快捷方式名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, " sina " );
// 设置是否允许重复创建快捷方式 false表示不允许
intent.putExtra( " duplicate " , false );
// 设置快捷方式要打开的intent
// 第一种方法创建快捷方式要打开的目标intent
Intent targetIntent = new Intent();
// 设置应用程序卸载时同时也删除桌面快捷方式
targetIntent.setAction(Intent.ACTION_MAIN);
targetIntent.addCategory( " android.intent.category.LAUNCHER " );
ComponentName componentName = new ComponentName(getPackageName(), this .getClass().getName());
targetIntent.setComponent(componentName);
// 第二种方法创建快捷方式要打开的目标intent
/*
* Intent
* targetIntent=getPackageManager().getLaunchIntentForPackage(getPackageName
* ());
*/
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
// 发送广播
sendBroadcast(intent);
Editor editor = sharedPreferences.edit();
editor.putBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS, true );
editor.commit();
}
/**
* 删除桌面快捷方式
*/
@Deprecated
private void tearDownShortCut() {
Intent intent = new Intent(DROP_SHORTCUT_ACTION);
// 指定要删除的shortcut名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, " sina " );
String appClass = getPackageName() + " . " + this .getLocalClassName();
ComponentName component = new ComponentName(getPackageName(), appClass);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setAction(Intent.ACTION_MAIN).setComponent(component));
sendBroadcast(intent);
}
}
AndroidManifest.xml
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ShortCutActivity extends Activity {
private static final String CREATE_SHORTCUT_ACTION = " com.android.launcher.action.INSTALL_SHORTCUT " ;
private static final String DROP_SHORTCUT_ACTION = " com.android.launcher.action.UNINSTALL_SHORTCUT " ;
private static final String PREFERENCE_KEY_SHORTCUT_EXISTS = " IsShortCutExists " ;
Button button;
// 获取默认的SharedPreferences
SharedPreferences sharedPreferences ;
// 从SharedPreferences获取是否存在快捷方式 若不存在返回false 程序第一次进来肯定返回false
boolean exists ;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences( this );
exists = sharedPreferences.getBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS, false );
// 创建桌面快捷方式
// 若第一次启动则创建,下次启动则不创建
if ( ! exists) {
setUpShortCut();
}
Intent intent = getIntent();
String action = intent.getAction();
// 长按桌面 创建快捷方式
if (Intent.ACTION_CREATE_SHORTCUT.equals(action)){
Intent shortCut = new Intent(Intent.ACTION_MAIN);
shortCut.setClassName( this , this .getClass().getName());
Intent data = new Intent();
data.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext( this , R.drawable.logo));
data.putExtra(Intent.EXTRA_SHORTCUT_NAME, " sina " );
data.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortCut);
data.putExtra( " duplicate " , false );
setResult(RESULT_OK, data);
finish();
return ;
}
setContentView(R.layout.main);
button = (Button) findViewById(R.id.dropShortCut);
button.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
tearDownShortCut();
}
});
}
/**
* 启动时创建桌面快捷方式
*/
private void setUpShortCut() {
Intent intent = new Intent(CREATE_SHORTCUT_ACTION);
// 设置快捷方式图片
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext( this , R.drawable.logo));
// 设置快捷方式名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, " sina " );
// 设置是否允许重复创建快捷方式 false表示不允许
intent.putExtra( " duplicate " , false );
// 设置快捷方式要打开的intent
// 第一种方法创建快捷方式要打开的目标intent
Intent targetIntent = new Intent();
// 设置应用程序卸载时同时也删除桌面快捷方式
targetIntent.setAction(Intent.ACTION_MAIN);
targetIntent.addCategory( " android.intent.category.LAUNCHER " );
ComponentName componentName = new ComponentName(getPackageName(), this .getClass().getName());
targetIntent.setComponent(componentName);
// 第二种方法创建快捷方式要打开的目标intent
/*
* Intent
* targetIntent=getPackageManager().getLaunchIntentForPackage(getPackageName
* ());
*/
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
// 发送广播
sendBroadcast(intent);
Editor editor = sharedPreferences.edit();
editor.putBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS, true );
editor.commit();
}
/**
* 删除桌面快捷方式
*/
@Deprecated
private void tearDownShortCut() {
Intent intent = new Intent(DROP_SHORTCUT_ACTION);
// 指定要删除的shortcut名称
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, " sina " );
String appClass = getPackageName() + " . " + this .getLocalClassName();
ComponentName component = new ComponentName(getPackageName(), appClass);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent().setAction(Intent.ACTION_MAIN).setComponent(component));
sendBroadcast(intent);
}
}
<?
xml version="1.0" encoding="utf-8"
?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package ="com.zhy.shortcut"
android:versionCode ="1"
android:versionName ="1.0" >
< uses-sdk android:minSdkVersion ="8" />
<!-- 创建桌面快捷方式的权限 -->
< uses-permission android:name ="com.android.launcher.permission.INSTALL_SHORTCUT" />
< uses-permission android:name ="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
< application
android:icon ="@drawable/logo"
android:label ="@string/app_name" >
< activity
android:label ="@string/app_name"
android:name =".ShortCutActivity" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
< activity-alias
android:targetActivity =".ShortCutActivity"
android:name =".AliasShortCutActivity" >
< intent-filter >
< action android:name ="android.intent.action.CREATE_SHORTCUT" />
< category android:name ="android.intent.category.DEFAULT" />
</ intent-filter >
</ activity-alias >
</ application >
</ manifest >
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package ="com.zhy.shortcut"
android:versionCode ="1"
android:versionName ="1.0" >
< uses-sdk android:minSdkVersion ="8" />
<!-- 创建桌面快捷方式的权限 -->
< uses-permission android:name ="com.android.launcher.permission.INSTALL_SHORTCUT" />
< uses-permission android:name ="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
< application
android:icon ="@drawable/logo"
android:label ="@string/app_name" >
< activity
android:label ="@string/app_name"
android:name =".ShortCutActivity" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
< activity-alias
android:targetActivity =".ShortCutActivity"
android:name =".AliasShortCutActivity" >
< intent-filter >
< action android:name ="android.intent.action.CREATE_SHORTCUT" />
< category android:name ="android.intent.category.DEFAULT" />
</ intent-filter >
</ activity-alias >
</ application >
</ manifest >