Android 開發之系統應用Launcher詳解,簡單添加和刪除快捷方式及常見問題
1.. Launcher是什么?
1.1 Launcher是系統啟動后加載的第一個應用程序
1.2 Launcher是其他應用程序的入口
2.Launcher的構成:

3. 主體四大組件的區別:
ShortCut: 應用程序的快捷方式
Appwidget:桌面小部件,圖形不規則
LiveFolder: 文件夾
以ContentProvider的形式展示應用中特定數據的集合
WallPaper: 壁紙
預覽圖如下:
4. 通過按鈕添加或者刪除應用程序的快捷方式Demo:
Intent.EXTRA_SHORTCUT_INTENT,
布局文件:
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/BT_InstallShortCurt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Install ShortCurt" />
<Button
android:id="@+id/BT_UnInstallShortCurt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="UnInstall ShortCurt" />
</LinearLayout>清單文件Mainifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.itheima.lancher"
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/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".LancherDemoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ShortCutActivity">
<intent-filter >
<!-攔截添加快捷方式,顯示土司->
<action android:name="android.intent.action.CREATE_SHORTCUT"/>
</intent-filter>
</activity>
</application>
</manifest>LancherDemoActivity;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class LancherDemoActivity extends Activity {
private Button button1;
private Button button2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.BT_InstallShortCurt);
button2 = (Button) findViewById(R.id.BT_UnInstallShortCurt);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
addShortCurt2DeskTop();
}
/**
* 添加桌面快捷方式
*/
private void addShortCurt2DeskTop() {
Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
//快捷方式的名稱
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TonyAutoShortCut");
shortcut.putExtra("duplicate", false); //不允許重復創建
shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(LancherDemoActivity.this, R.drawable.beauty));
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(LancherDemoActivity.this,LancherDemoActivity.class).setAction("com.android.action.test"));
//發送廣播
sendBroadcast(shortcut);
}
});
/**
* 刪除桌面快捷方式
*/
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
deleteShortCurt2DeskTop();
}
private void deleteShortCurt2DeskTop() {
Intent shortcut = new Intent("com.android.launcher.action.UNINSTALL_SHORTCUT");
//快捷方式的名稱
shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, "TonyAutoShortCut");
shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, new Intent(LancherDemoActivity.this,LancherDemoActivity.class).setAction("com.android.action.test"));
sendBroadcast(shortcut);
}
});
}
}注意事項: 在設置 EXTRA_SHORTCUT_INTENT時,添加和刪除快捷方式的這個INTENT對象的ACTION屬性必須設置為相同內容,才能聲明刪除和創建的快捷方式是一個,進行綁定,
否則刪除無法生效
本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!