在Android中創(chuàng)建ShortCut大概有兩種方法。
第一種方法就是參照api demos中寫的那個(gè),通過(guò)設(shè)置setResult(RESULT_OK, intent);來(lái)創(chuàng)建ShortCut,這種方式在稍后分析。
本文以Broadcast方式方式來(lái)介紹Android中ShortCut的創(chuàng)建。
在創(chuàng)建或刪除ShortCut的時(shí)候先需要在AndroidManifest.xml中增加兩個(gè)權(quán)限
<!-- 創(chuàng)建桌面快捷方式的權(quán)限 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
另外記得在創(chuàng)建或刪除ShortCut的Intent中設(shè)置Action為
com.android.launcher.action.INSTALL_SHORTCUT(創(chuàng)建)
com.android.launcher.action.UNINSTALL_SHORTCUT(刪除)
這樣發(fā)送出去的廣播才能被Android系統(tǒng)接受到
詳細(xì)的代碼:
注意:該方式創(chuàng)建的ShortCut會(huì)在啟動(dòng)應(yīng)用的時(shí)候就創(chuàng)建ShortCut。下一講中的創(chuàng)建方式,僅僅只是在長(zhǎng)按Android桌面后你的當(dāng)前應(yīng)用創(chuàng)建的快捷方式可以在這里檢索到,需要你手動(dòng)創(chuàng)建出來(lái)。
第一種方法就是參照api demos中寫的那個(gè),通過(guò)設(shè)置setResult(RESULT_OK, intent);來(lái)創(chuàng)建ShortCut,這種方式在稍后分析。
本文以Broadcast方式方式來(lái)介紹Android中ShortCut的創(chuàng)建。
在創(chuàng)建或刪除ShortCut的時(shí)候先需要在AndroidManifest.xml中增加兩個(gè)權(quán)限
<!-- 創(chuàng)建桌面快捷方式的權(quán)限 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
另外記得在創(chuàng)建或刪除ShortCut的Intent中設(shè)置Action為
com.android.launcher.action.INSTALL_SHORTCUT(創(chuàng)建)
com.android.launcher.action.UNINSTALL_SHORTCUT(刪除)
這樣發(fā)送出去的廣播才能被Android系統(tǒng)接受到
詳細(xì)的代碼:
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;
// 獲取默認(rèn)的SharedPreferences
SharedPreferences sharedPreferences ;
// 從SharedPreferences獲取是否存在快捷方式 若不存在返回false 程序第一次進(jìn)來(lái)肯定返回false
boolean exists ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
exists = sharedPreferences.getBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS, false);
//創(chuàng)建桌面快捷方式
//若第一次啟動(dòng)則創(chuàng)建,下次啟動(dòng)則不創(chuàng)建
if (!exists) {
setUpShortCut();
}
setContentView(R.layout.main);
button = (Button) findViewById(R.id.dropShortCut);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tearDownShortCut();
}
});
}
/**
* 創(chuàng)建桌面快捷方式
*/
private void setUpShortCut() {
Intent intent = new Intent(CREATE_SHORTCUT_ACTION);
// 設(shè)置快捷方式圖片
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this, R.drawable.logo));
// 設(shè)置快捷方式名稱
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "sina");
// 設(shè)置是否允許重復(fù)創(chuàng)建快捷方式 false表示不允許
intent.putExtra("duplicate", false);
// 設(shè)置快捷方式要打開(kāi)的intent
// 第一種方法創(chuàng)建快捷方式要打開(kāi)的目標(biāo)intent
Intent targetIntent = new Intent();
// 設(shè)置應(yīng)用程序卸載時(shí)同時(shí)也刪除桌面快捷方式
targetIntent.setAction(Intent.ACTION_MAIN);
targetIntent.addCategory("android.intent.category.LAUNCHER");
ComponentName componentName = new ComponentName(getPackageName(), this.getClass().getName());
targetIntent.setComponent(componentName);
// 第二種方法創(chuàng)建快捷方式要打開(kāi)的目標(biāo)intent
/*
* Intent
* targetIntent=getPackageManager().getLaunchIntentForPackage(getPackageName
* ());
*/
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
// 發(fā)送廣播
sendBroadcast(intent);
Editor editor = sharedPreferences.edit();
editor.putBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS, true);
editor.commit();
}
/**
* 刪除桌面快捷方式
*/
private void tearDownShortCut() {
Intent intent = new Intent(DROP_SHORTCUT_ACTION);
// 指定要?jiǎng)h除的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;
// 獲取默認(rèn)的SharedPreferences
SharedPreferences sharedPreferences ;
// 從SharedPreferences獲取是否存在快捷方式 若不存在返回false 程序第一次進(jìn)來(lái)肯定返回false
boolean exists ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
exists = sharedPreferences.getBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS, false);
//創(chuàng)建桌面快捷方式
//若第一次啟動(dòng)則創(chuàng)建,下次啟動(dòng)則不創(chuàng)建
if (!exists) {
setUpShortCut();
}
setContentView(R.layout.main);
button = (Button) findViewById(R.id.dropShortCut);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tearDownShortCut();
}
});
}
/**
* 創(chuàng)建桌面快捷方式
*/
private void setUpShortCut() {
Intent intent = new Intent(CREATE_SHORTCUT_ACTION);
// 設(shè)置快捷方式圖片
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this, R.drawable.logo));
// 設(shè)置快捷方式名稱
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "sina");
// 設(shè)置是否允許重復(fù)創(chuàng)建快捷方式 false表示不允許
intent.putExtra("duplicate", false);
// 設(shè)置快捷方式要打開(kāi)的intent
// 第一種方法創(chuàng)建快捷方式要打開(kāi)的目標(biāo)intent
Intent targetIntent = new Intent();
// 設(shè)置應(yīng)用程序卸載時(shí)同時(shí)也刪除桌面快捷方式
targetIntent.setAction(Intent.ACTION_MAIN);
targetIntent.addCategory("android.intent.category.LAUNCHER");
ComponentName componentName = new ComponentName(getPackageName(), this.getClass().getName());
targetIntent.setComponent(componentName);
// 第二種方法創(chuàng)建快捷方式要打開(kāi)的目標(biāo)intent
/*
* Intent
* targetIntent=getPackageManager().getLaunchIntentForPackage(getPackageName
* ());
*/
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, targetIntent);
// 發(fā)送廣播
sendBroadcast(intent);
Editor editor = sharedPreferences.edit();
editor.putBoolean(PREFERENCE_KEY_SHORTCUT_EXISTS, true);
editor.commit();
}
/**
* 刪除桌面快捷方式
*/
private void tearDownShortCut() {
Intent intent = new Intent(DROP_SHORTCUT_ACTION);
// 指定要?jiǎng)h除的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" />
<!-- 創(chuàng)建桌面快捷方式的權(quán)限 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
<application
android:icon="@drawable/ic_launcher"
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>
</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" />
<!-- 創(chuàng)建桌面快捷方式的權(quán)限 -->
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>
<application
android:icon="@drawable/ic_launcher"
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>
</application>
</manifest>
注意:該方式創(chuàng)建的ShortCut會(huì)在啟動(dòng)應(yīng)用的時(shí)候就創(chuàng)建ShortCut。下一講中的創(chuàng)建方式,僅僅只是在長(zhǎng)按Android桌面后你的當(dāng)前應(yīng)用創(chuàng)建的快捷方式可以在這里檢索到,需要你手動(dòng)創(chuàng)建出來(lái)。