创建快捷方式的主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(this, this.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系统检索到