Android 主界面长按创建快捷方式

Android中创建快捷方式主要有两种方式。一是在代码中直接加入生成桌面快捷方式的代码;二是通过小部件加入;

这篇文章主要讲另外一种方法!

1、通过在AndroidManifest文件里为Activity加入

<intent-filter>                    
            <action android:name="android.intent.action.CREATE_SHORTCUT"/>                
</intent-filter>

加入之后。长按桌面,小部件中会有你应用的图标出现!这才完毕了第一步!


2、通过第一步之后,你是不可以把快捷方式拖到桌面上的,仅仅会进入到指定的Activity。事实上生成图标是在Activity内用代码完毕的!

在oncreate中首先推断是否是创建快捷方式的action:

private boolean isCreateShortcut(Intent intent){
		return SHORCUTACTION.equals(intent.getAction())?true:false;
	}

假设是的话就创建快捷方式:

	private void creareShorcut(){
		Log.e(TAG, " ---------creareShorcut----------");
		Intent addShortCut = new Intent();                 
        //快捷方式的名称                 
        addShortCut.putExtra(Intent.EXTRA_SHORTCUT_NAME , "快捷方式名称");                 
        //桌面上显示的图片                
        Parcelable icon = ShortcutIconResource.fromContext(this, R.drawable.cacaxi);                 
        addShortCut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);     
        //点击快捷方式时,须要启动的Activity,可是应该是该应用的主Activity;或者也能够是到网址。电话等         
        Intent toIntent = new Intent(mContext,MainActivity.class);
        Intent call112 =   
                new Intent(Intent.ACTION_CALL,Uri.parse("tel://112")); 
        addShortCut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,toIntent);                 
        //最后一步,发送                
        setResult(RESULT_OK, addShortCut); 
	}

须要注意的是启动的Activity。应该是该应用的主Activity,由于我用其它的Activity试了非常多的方式也不可以启动,没有深入的去研究。假设有谁研究过可以分享下!

那如今就有个问题了。怎么才干到指定的Activity?

答案就是在Intent中加入參数。通过主Activity来中转。




原文地址:https://www.cnblogs.com/mthoutai/p/6963879.html