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

          雪山飛鵠

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

          BlogJava 首頁 新隨筆 聯系 聚合 管理
            215 Posts :: 1 Stories :: 674 Comments :: 0 Trackbacks
          創建快捷方式的主Activity
          package com.zhy.weather;

          import android.app.Activity;
          import android.content.Intent;
          import android.os.Bundle;
          import android.os.Handler;
          import android.os.Parcelable;

          public class SplashActivity extends Activity {
              
              @Override
              
          protected void onCreate(Bundle savedInstanceState) {
                  
          super.onCreate(savedInstanceState);
                  
                  
          final Intent intent=getIntent();
                  
          final String action=intent.getAction();
                  
                  
          //設置允許創建快捷方式
                  if(Intent.ACTION_CREATE_SHORTCUT.equals(action)){
                      setupShortcut();
                      finish();
                      
          return;
                  }
                  
                  
                  setContentView(R.layout.splash);
                  
          new Handler().postDelayed(new Runnable() {
                      
                      @Override
                      
          public void run() {
                          SplashActivity.
          this.startActivity(new Intent(SplashActivity.this, MainActivity.class));
                          SplashActivity.
          this.finish();
                      }
                  }, 
          2000);
              }

              
          //創建快捷方式
              private void setupShortcut() {
                  
          //目標Intent 打開快捷方式要啟動的那個intent
                  Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
                  shortcutIntent.setClassName(
          thisthis.getClass().getName());

                  
          // Then, set up the container intent (the response to the caller)

                  Intent intent 
          = new Intent();
                  intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
                  intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, 
          "小程序");
                  Parcelable iconResource 
          = Intent.ShortcutIconResource.fromContext(this,  R.drawable.app);
                  intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

                  
          // Now, return the result to the launcher

                  setResult(RESULT_OK, intent);
              }
              
              
              
          }

          AndroidManifest.xml
          <activity
                      
          android:name=".SplashActivity" >
                      
          <intent-filter >
                          
          <action android:name="android.intent.action.MAIN" />
                          
          <category android:name="android.intent.category.LAUNCHER" />
                      
          </intent-filter>
                  
          </activity>
                  
                  
          <!-- 創建桌面快捷方式 -->            
                  
          <activity-alias android:name=".CreateShortcuts"
                      android:targetActivity
          =".SplashActivity">
                      
          <intent-filter>
                          
          <action android:name="android.intent.action.CREATE_SHORTCUT" />
                          
          <category android:name="android.intent.category.DEFAULT" />
                      
          </intent-filter>
                  
          </activity-alias>   
          重點是注意activity-alias中的部分
          android:name 就是取個別名的意思
          android:targetActivity=".SplashActivity" 指定目標Activity
          <action android:name="android.intent.action.CREATE_SHORTCUT" />指定該action才可以被android系統檢索到

          posted on 2011-12-13 14:03 雪山飛鵠 閱讀(1401) 評論(0)  編輯  收藏 所屬分類: android
          主站蜘蛛池模板: 石台县| 惠水县| 衡水市| 盐池县| 南昌县| 临汾市| 黄陵县| 通海县| 当雄县| 兴安盟| 嵩明县| 股票| 乐安县| 古蔺县| 渭源县| 仪征市| 荥阳市| 台前县| 富源县| 松滋市| 汝州市| 曲阜市| 丰顺县| 凭祥市| 左云县| 游戏| 凉山| 岳池县| 遂溪县| 高唐县| 泰州市| 石阡县| 惠东县| 同仁县| 益阳市| 阳曲县| 饶河县| 株洲县| 襄汾县| 德清县| 惠安县|