溫馨提示:您的每一次轉載,體現了我寫此文的意義!!!煩請您在轉載時注明出處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 雪山飛鵠 閱讀(1397) 評論(0)  編輯  收藏 所屬分類: android
          主站蜘蛛池模板: 高安市| 广德县| 本溪市| 乐业县| 鄂托克前旗| 冀州市| 洮南市| 长葛市| 台南县| 贺兰县| 册亨县| 兴业县| 太白县| 长葛市| 澄城县| 女性| 福贡县| 叙永县| 绥德县| 博野县| 林芝县| 奉贤区| 嘉义县| 麻江县| 繁昌县| 湄潭县| 仁寿县| 分宜县| 肃南| 阳原县| 广河县| 安顺市| 锡林浩特市| 鄱阳县| 乌鲁木齐县| 开远市| 镇远县| 乃东县| 拉萨市| 绵竹市| 右玉县|