动态设置 shortCut

转自: http://www.maxiaoguo.com/shipin/345.html

纠结了好几天,昨天终于做出来了,总结下。


上图



点击图标


先说shortcut怎么创建吧

1、先在AndroidManifest.xml中配置下

  这是跳转到ScDialogActivity的配置 实际上此activity就是上图的窗口

 <activity android:name=".ui.ScDialogActivity"
           android:screenOrientation="portrait"
           android:theme="@style/scDialogTheme"     
           android:process=":com.mgamecenter.sc" 
           android:taskAffinity="com.m4399.shortcut"    注释1:
           >
             <intent-filter>   //注释4:
                <action android:name="m4399.ui.ScDialogActivity.sc"/>
                <categoryandroid:name=".ScDialogActivity" />
                <categoryandroid:name="android.intent.category.DEFAULT"/>
            </intent-filter>
   </activity>


style.xml中

 

<stylename="scDialogTheme" parent="android:Theme.Dialog"> //注释3:
        <itemname="android:windowFrame">@null</item>
        <itemname="android:windowIsFloating">true</item>
        <itemname="android:windowIsTranslucent">false</item> 
        <item name="android:windowNoTitle">true</item><!--除去title-->
        <itemname="android:windowContentOverlay">@null</item> 
        <itemname="android:backgroundDimEnabled">false</item>
         <itemname="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowBackground">@color/transparent</item><!--除去背景色-->  //注释2:
         </style>

注释1: 注意 这是另外起一个任务,可以说进程   长按HOME的时候 就会看见两个此应用的图标了  

              android:taskAffinity不加这个的话 当此应用通过home推出的时候 ,点击shortcut的时候在弹出窗口的时候也会启动主程序
注释2: 注意 <itemname="android:windowBackground">@null</item>   当属性设置null的时候 窗口的背景不会显示透明度,会显示黑色,

                   设置成transparent就正常了

注释3:activity要想成为窗口,得加个属性<stylename="scDialogTheme" parent="android:Theme.Dialog"> 就是他

注释4:  设置拦截器 ,这里需要和activity配合 着 使用

 

2、在启动的的activityjava文件中写入创建shortcut的代码

在create方法中调用此方法

</pre><p></p><pre name="code" class="java">public void createSc() {
		//firstScLaunch = Session.get(this).isFirstScLaunch();
		//if (firstScLaunch) {
		dao = new NativeGameDao(this);
		allGame = dao.getAllGame();   //拿到本地的应用   当没有数据的时候不创建
			if (hasSc()||allGame.size()==0) {
				return;
			}
			addSc();
		//}
	}
// 判断是否已经创建了游戏盒快捷方式
	private boolean hasSc() {
		boolean isInstallShortcut = false;
		final ContentResolver cr = getContentResolver();
		final String AUTHORITY = "com.android.launcher.settings";
		final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
				+ "/favorites?notify=true");
		Cursor c = cr.query(CONTENT_URI,
				new String[] { "title", "iconResource" }, "title=?",
				new String[] { getString(R.string.sc_name).trim() }, null);
		if (c != null && c.getCount() > 0) {
			isInstallShortcut = true;
		}
		if (c != null) {
			c.close();
		}
		return isInstallShortcut;
	}

这个原理不知道咋验证的,待考究

/**
	 * 为程序创建游戏盒子快捷方式
	 */
	private void addSc() {
		Intent shortcut = new Intent(
				"com.android.launcher.action.INSTALL_SHORTCUT");
		// 快捷方式的名称
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME,
				getString(R.string.sc_name));
		shortcut.putExtra("duplicate", false); // 不允许重复创建


		Intent intent = new Intent();
		intent.setAction("m4399.ui.ScDialogActivity.sc");   //注释1:
		intent.addCategory(".ScDialogActivity");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);  
		 //快捷方式的图标
		Bitmap bmp = drawBitMap();
		shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,bmp);    //注释4:
		sendBroadcast(shortcut);
	}

//生成bitmap图片
	private Bitmap drawBitMap() {
		Resources r = getApplication().getResources();
		InputStream is = r.openRawResource(R.drawable.game_frame_sc_back);
		BitmapDrawable  bmpDraw = new BitmapDrawable(is);
		Bitmap bitmap = bmpDraw.getBitmap().copy(Bitmap.Config.ARGB_8888, true);
		Canvas canvas = new Canvas(bitmap);     //注释2:
		Paint mpaint = new Paint();
		for(int i=0;i<5;i++){
			if(allGame!=null&&allGame.size()>i){
				GameInfo gameInfo = allGame.get(i);
				if(gameInfo!=null){
					Drawable drawableUnder = AppUtils.getIconFromPKname(getApplicationContext(), gameInfo.packageName);
					if(drawableUnder!=null){
						BitmapDrawable bitmapDrawableUnder = (BitmapDrawable)drawableUnder;
						Bitmap bitmapUnder = bitmapDrawableUnder.getBitmap().copy(Bitmap.Config.ARGB_8888, true);    //注释3:
						Bitmap createScaledBitmap = Bitmap.createScaledBitmap(bitmapUnder,45,45,true);
						canvas.drawBitmap(createScaledBitmap,25-4*i,i*4+5 , mpaint);
						canvas.save(Canvas.ALL_SAVE_FLAG);
						// 存储新合成的图片
						canvas.restore();   
					}
				}  
			}  
		}
		InputStream isFront = r.openRawResource(R.drawable.game_frame_sc_front);
		BitmapDrawable  bmpDrawFront = new BitmapDrawable(isFront);
		Bitmap bitmapFront = bmpDrawFront.getBitmap().copy(Bitmap.Config.ARGB_8888, true);
		canvas.drawBitmap(bitmapFront,0,0, mpaint);
		canvas.save(Canvas.ALL_SAVE_FLAG);
		// 存储新合成的图片
		canvas.restore();
		return bitmap;
	}


 //注释1:这里配合配置文件里使用

//注释2:Canvascanvas = new Canvas(bitmap);  是在bitmap上画图。。要是canvas.drawBitmap是在canvas上画图,这里需要一个bitmap所以要在bitmap上画

//注释3:copy(Bitmap.Config.ARGB_8888,true);  这个一定一定要加。。因为是调用系统的图片合成的,基于资源文件不能修改的原则,所有得copy一下,要不会报异常

//注释4: shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON,bmp);   这里bmp 直接传bitmap就行,之前以为bitmap还需要parceble序列化一下呢,弄了好久没弄好,原来 

bitmap是已经序列化的

 

 

大功告成。。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下是 Android 动态创建快捷方式的步骤: 1. 首先,您需要在 AndroidManifest.xml 文件中声明您的快捷方式。在应用程序的 <application> 标记内部,添加以下内容: ```xml <activity android:name=".MyShortcutActivity" android:label="@string/shortcut_label"> <intent-filter> <action android:name="android.intent.action.CREATE_SHORTCUT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </activity> ``` 2. 创建一个新的 Activity 类 MyShortcutActivity,该类将处理创建快捷方式的请求。在 onCreate() 方法中,您可以设置快捷方式的属性,例如快捷方式 ID、快捷方式标签和快捷方式图标。 ```java public class MyShortcutActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置快捷方式 ID 和标签 String shortcutId = "my_shortcut"; String shortcutLabel = "My Shortcut"; // 创建快捷方式意图 Intent shortcutIntent = new Intent(Intent.ACTION_VIEW); shortcutIntent.setClassName(this, MainActivity.class.getName()); // 创建快捷方式 ShortcutInfo shortcut = new ShortcutInfo.Builder(this, shortcutId) .setShortLabel(shortcutLabel) .setIcon(Icon.createWithResource(this, R.drawable.shortcut_icon)) .setIntent(shortcutIntent) .build(); // 添加快捷方式 ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut)); // 结束 Activity finish(); } } ``` 3. 在您的应用程序中,您可以通过调用 ShortcutManager 的 setDynamicShortcuts() 方法来添加动态快捷方式。在这个例子中,我们只添加了一个快捷方式,但您可以添加多个快捷方式。 ```java ShortcutManager shortcutManager = getSystemService(ShortcutManager.class); shortcutManager.setDynamicShortcuts(Collections.singletonList(shortcut)); ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值