Android創建桌面快捷方式幾種方法
Android在桌面上生成快捷方式有兩種情況,一種是直接在桌面直接生成;一種是長按桌面,在彈出的快捷菜單中生成。
談談在桌面上直接生成。個人覺得這個比較爽快,既然都是快捷方式了干嘛還要再隱藏一層呢?當然喜歡桌面干凈的就比較喜歡第二個了。
第一個是通過廣播(Broadcast)的形式向Luncher發送請求生成快捷方式的。
在網上找到關于這方面的注冊信息。
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < ! -- 設置wallpapaer的activity -->
< ! -- Intent received used to install shortcuts from other applications -->
< receiver
android:name = " com.android.launcher2.InstallShortcutReceiver "
android:permission = " com.android.launcher.permission.INSTALL_SHORTCUT " >
< intent - filter >
< action android:name = " com.android.launcher.action.INSTALL_SHORTCUT " />
</ intent - filter >
</ receiver >
</div>
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < ! -- 設置wallpapaer的activity -->
< ! -- Intent received used to install shortcuts from other applications -->
< receiver
android:name = " com.android.launcher2.InstallShortcutReceiver "
android:permission = " com.android.launcher.permission.INSTALL_SHORTCUT " >
< intent - filter >
< action android:name = " com.android.launcher.action.INSTALL_SHORTCUT " />
</ intent - filter >
</ receiver >
可以看出,要在桌面上創建快捷方式就需要權限了:
android:permission="com.android.launcher.permission.INSTALL_SHORTCUT。
所以在我們的manifest.xml文件中,我們需要加入下面這段話:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < uses - permission android:name = " com.android.launcher.permission.INSTALL_SHORTCUT " />
</div>
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < uses - permission android:name = " com.android.launcher.permission.INSTALL_SHORTCUT " />
下面就是代碼層的實現:
假如我在一個activity中創建一個創建快捷方式的方法:createShortCut();
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> public void createShortCut(){
// 創建快捷方式的Intent
Intent shortcutintent = new Intent( " com.android.launcher.action.INSTALL_SHORTCUT " );
// 不允許重復創建
shortcutintent.putExtra( " duplicate " , false );
// 需要現實的名稱
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
// 快捷圖片
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 點擊快捷圖片,運行的程序主入口
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , EnterActivity.class));
// 發送廣播。OK
sendBroadcast(shortcutintent);
}
</div>
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> public void createShortCut(){
// 創建快捷方式的Intent
Intent shortcutintent = new Intent( " com.android.launcher.action.INSTALL_SHORTCUT " );
// 不允許重復創建
shortcutintent.putExtra( " duplicate " , false );
// 需要現實的名稱
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.shortcutname));
// 快捷圖片
Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon);
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 點擊快捷圖片,運行的程序主入口
shortcutintent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(getApplicationContext() , EnterActivity.class));
// 發送廣播。OK
sendBroadcast(shortcutintent);
}
二、長按桌面彈出的桌面快捷方式創建
第一頁談過直接在桌面生成快捷方式,現在說說如何在添加到一個SHORTCUTS列表中,就是你長按桌面彈出來的那個東東。
首先在注冊activity時,需要添加一個action為android.intent.action.CREATE_SHOERTCUT的intentFilter.如下所示:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < activity android:name = " ShortCutTest " >
< intent - filter >
< action android:name = " android.intent.action.CREATE_SHORTCUT " />
</ intent - filter >
</ activity >
</div>
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> < activity android:name = " ShortCutTest " >
< intent - filter >
< action android:name = " android.intent.action.CREATE_SHORTCUT " />
</ intent - filter >
</ activity >
接下來就是就是設置快捷方式的圖標、名稱、事件等屬性。這里圖表的生成,android里提供了專門的方法來生成。
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> public class ShortCutTest extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto - generated method stub
super.onCreate(savedInstanceState);
}
public void createShortCut(){
Intent addShortCut;
// 判斷是否需要添加快捷方式
if (getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)){
addShortCut = new Intent();
// 快捷方式的名稱
addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME , " 我的快捷方式 " );
// 顯示的圖片
Parcelable icon = ShortcutIconResource.fromContext(this, R.drawable.icon);
addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 快捷方式激活的activity,需要執行的intent,自己定義
addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent());
// OK,生成
setResult(RESULT_OK, addShortCut);
} else {
// 取消
setResult(RESULT_CANCELED);
}
}
}
</div>
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
--> public class ShortCutTest extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto - generated method stub
super.onCreate(savedInstanceState);
}
public void createShortCut(){
Intent addShortCut;
// 判斷是否需要添加快捷方式
if (getIntent().getAction().equals(Intent.ACTION_CREATE_SHORTCUT)){
addShortCut = new Intent();
// 快捷方式的名稱
addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME , " 我的快捷方式 " );
// 顯示的圖片
Parcelable icon = ShortcutIconResource.fromContext(this, R.drawable.icon);
addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
// 快捷方式激活的activity,需要執行的intent,自己定義
addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent());
// OK,生成
setResult(RESULT_OK, addShortCut);
} else {
// 取消
setResult(RESULT_CANCELED);
}
}
}
本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!