Android應用創建手機桌面快捷方式

jopen 9年前發布 | 992 次閱讀 Java Android

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&quot;
    xmlns:tools="http://schemas.android.com/tools&quot;
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:orientation="vertical">
    <Button 
        android:id="@+id/shortCut_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    android:text="快捷方式"
        />
    <Button
        android:id="@+id/exit_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="退出應用" />
</LinearLayout>

---------------------------------------------------------------------------
public class MainActivity extends Activity {
private Button shortCutBtn;
private Button exitBtn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shortCutBtn = (Button) findViewById(R.id.shortCut_btn);
exitBtn = (Button) findViewById(R.id.exit_btn);
/創建 快捷方式*/
shortCutBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
/創建或刪除ShortCut的Intent中設置Action為:com.android.launcher.action.INSTALL_SHORTCUT

  • 另外刪除快捷方式為:com.android.launcher.action.UNINSTALL_SHORTCUT / Intent addIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT"); /**Android專門提供了Intent.ShortcutIconResource.fromContext來創建快捷方式的圖標,最后通過setResult來返回/ Parcelable icon = Intent.ShortcutIconResource.fromContext(MainActivity.this, R.drawable.ic_launcher); Intent myIntent = new Intent(MainActivity.this, MainActivity.class); /應用名稱*/ addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "應用名稱"); /應用圖片/ addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon); /**應用啟動Intent/ addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, myIntent); sendBroadcast(addIntent); } }); /*退出應用/ exitBtn.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { System.exit(0); } }); }

}</pre>

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!