admin管理员组文章数量:1566641
转自: http://www.maxiaoguo/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、在启动的的activity的java文件中写入创建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是已经序列化的
大功告成。。
版权声明:本文标题:动态设置shortCut 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/dianzi/1725690665a1037025.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论