溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          雪山飛鵠

          溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處http://www.aygfsteel.com/sxyx2008/謝謝合作!!!

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
          在Android中創建ShortCut大概有兩種方法。
          第一種方法就是參照api demos中寫的那個,通過設置setResult(RESULT_OK, intent);來創建ShortCut,這種方式在稍后分析。
          本文以Broadcast方式方式來介紹Android中ShortCut的創建。
          在創建或刪除ShortCut的時候先需要在AndroidManifest.xml中增加兩個權限
          <!-- 創建桌面快捷方式的權限 -->
          <uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
          <uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT"/>

          另外記得在創建或刪除ShortCut的Intent中設置Action為
          com.android.launcher.action.INSTALL_SHORTCUT(創建)
          com.android.launcher.action.UNINSTALL_SHORTCUT(刪除)
          這樣發送出去的廣播才能被Android系統接受到

          詳細的代碼:
          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();
                  }
                  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();

              }

              
          /**
               * 刪除桌面快捷方式
               
          */
              
          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
          <?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/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>

          注意:該方式創建的ShortCut會在啟動應用的時候就創建ShortCut。下一講中的創建方式,僅僅只是在長按Android桌面后你的當前應用創建的快捷方式可以在這里檢索到,需要你手動創建出來。
          posted on 2011-12-13 13:56 雪山飛鵠 閱讀(4737) 評論(0)  編輯  收藏 所屬分類: android
          主站蜘蛛池模板: 洛隆县| 南投县| 安义县| 苍溪县| 冕宁县| 海伦市| 仙桃市| 彰化市| 朝阳市| 西峡县| 仁化县| 定襄县| 朔州市| 南康市| 白城市| 沛县| 任丘市| 定襄县| 高台县| 陕西省| 郁南县| 田林县| 望江县| 长阳| 弥勒县| 淅川县| 平山县| 山阳县| 新建县| 平昌县| 乌兰浩特市| 灵丘县| 陆川县| 阿图什市| 定边县| 收藏| 江西省| 平邑县| 六盘水市| 法库县| 固始县|