温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/sxyx2008/谢谢合作!!!

雪山飞鹄

温馨提示:您的每一次转载,体现了我写此文的意义!!!烦请您在转载时注明出处http://www.blogjava.net/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 雪山飞鹄 阅读(1375) 评论(0)  编辑  收藏 所属分类: android

只有注册用户登录后才能发表评论。


网站导航: