Android之ShortCut[setResult方式]
16lz
2022-06-12
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系统检索到
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系统检索到
更多相关文章
- 目标身高Android版
- AIDL(android 接口定义语言)
- 天天记录 - Android创建应用窗口分析2
- android 选择本地图片并截剪图片保存到,sdcard指定目录中
- Android(安卓)图片裁剪之三剑式(二)
- android开发环境搭建备忘
- android 布局大全
- android布局动画实现方法
- Android(安卓)Virtual Device Manager 创建虚拟机出现SDK Manage