Android之ShortCut[setResult方式] 创建快捷方式的主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(BundlesavedInstanceState){
super .onCreate(savedInstanceState);

final Intentintent = getIntent();
final Stringaction = 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
IntentshortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setClassName(
this , this .getClass().getName());

// Then,setupthecontainerintent(theresponsetothecaller)

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

// Now,returntheresulttothelauncher

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系统检索到

更多相关文章

  1. 目标身高Android版
  2. AIDL(android 接口定义语言)
  3. 天天记录 - Android创建应用窗口分析2
  4. android 选择本地图片并截剪图片保存到,sdcard指定目录中
  5. Android(安卓)图片裁剪之三剑式(二)
  6. android开发环境搭建备忘
  7. android 布局大全
  8. android布局动画实现方法
  9. Android(安卓)Virtual Device Manager 创建虚拟机出现SDK Manage

随机推荐

  1. Android(安卓)的网络编程(1)-socket的用
  2. Android Notification通知栏的必备姿势
  3. Android: 发送虚拟按键
  4. Android--Android四大组件(一)(Android s
  5. Android设计模式系列--工厂方法模式
  6. android之ExoPlayer探索
  7. Plugin Error: required plugin “Androi
  8. 【Android】SlidingMenu属性详解
  9. Android(安卓)Studio加载字体
  10. Android与H5的交互