ダイナミック設定shortCut

6042 ワード

変換元:http://www.maxiaoguo.com/shipin/345.html
何日も悩んだあげく、昨日やっとできたので、まとめました.
上図
动态设置 shortCut_第1张图片
アイコンをクリック
动态设置 shortCut_第2张图片
まずshortcutの作成方法についてお話ししましょう
1、まずAndroidManifest.xmlでの構成
これはScDialogActivityにジャンプする構成です実際にはこのactivityが上図のウィンドウです
 
                //  4:
                
                
                
            
   

style.xmlで
 
 //  3:
        @null
        true
        false 
        true
        @null 
        false
         @null
        @color/transparent  //  2:
         

注1:注意これは別のタスクで、プロセスがHOMEを長押ししている間にこのアプリケーションのアイコンが2つ見えると言えます
android:taskAffinityこれを付けないとこのアプリがホームを通じて発売されたときにshortcutをクリックするとポップアップウィンドウでメインプログラム注釈2が起動します:@nullプロパティがnullに設定されているときにウィンドウの背景に透明度が表示されず、黒が表示されますので、
transparentに設定すれば正常です
注記3:activityウィンドウになるには、プロパティを付ける必要があります.
注記4:ブロッキングを設定します.ここではactivityと組み合わせて使用する必要があります.
 
2、起動したactivityのjavaファイルにshortcutを作成するコードを書き込む
createメソッドでこのメソッドを呼び出す
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

 

 

。。