安卓快捷图标创建方法

主要讲桌面快捷图标的动态生成:

public static void createDesk(final Context context,final String type){
		JsonUtils.desktOper(context, type, "");
		
		new Handler().postDelayed(new Runnable() { 
			@Override 
			public void run() { 
				downloadFile(SharedPreferencesUtil.getStringData(context, "icoImg", ""), context);
//				boolean isFirst = SharedPreferencesUtil.getBooleanData(context, "first", true);//判断如果是第一次创建桌面快捷图标,就创建
				if (!TextUtils.isEmpty(SharedPreferencesUtil.getStringData(context, "icoImg", ""))) {
					ArrayList<String> appNames = PackageUtils.getAppName(context);
			        Intent addIntent=new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
				    addIntent.putExtra("duplicate", false);
					addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, SharedPreferencesUtil.getStringData(context, "namedesk", ""));
					
					if (bitmap != null) {
						Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 128, 128, true);
						addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON, scaledBitmap);
					}
					
					if ("1".equals(type) && appNames.contains(SharedPreferencesUtil.getStringData(context, "packageName", "")) == false) {	
						Intent value = new Intent();
				        value.setAction("com.ys.update.activity.NotificationUpdateActivity");
				        value.addCategory("android.intent.category.DEFAULT");
				        value.putExtra("url", SharedPreferencesUtil.getStringData(context, "urldesk", ""));
						addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,value);
						context.sendBroadcast(addIntent);
//							SharedPreferencesUtil.saveBooleanData(context, "first", false);
						SharedPreferencesUtil.saveStringData(context, "downloadWay", "5");
//							Toast.makeText(context, "已创建桌面图标", 0).show();
					}else{
						Uri uri = Uri.parse(SharedPreferencesUtil.getStringData(context, "neturl", ""));
					    Intent intent = new Intent(Intent.ACTION_VIEW, uri);
						addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,intent);
						context.sendBroadcast(addIntent);
//						SharedPreferencesUtil.saveBooleanData(context, "first", false);
//						Toast.makeText(context, "已创建桌面图标", 0).show();
					}
				} 
			}
		}, 2000);
	}



 /** 
     * 显示网络上的图片
     * 把图片链接转为 Bitmap
     * @param url    图片的链接
     * @return
     */
   private static Bitmap bitmap; 
<span style="white-space:pre">	</span>public static void downloadFile(final String uri,final Context context) {
<span style="white-space:pre">		</span>new Thread() {
<span style="white-space:pre">			</span>
<span style="white-space:pre">			</span>public void run() {
<span style="white-space:pre">				</span>// 这里下载数据
<span style="white-space:pre">				</span>try {
<span style="white-space:pre">					</span>URL url = new URL(uri);
<span style="white-space:pre">					</span>HttpURLConnection conn = (HttpURLConnection) url.openConnection();
<span style="white-space:pre">					</span>conn.setDoInput(true);
<span style="white-space:pre">					</span>conn.connect();
<span style="white-space:pre">					</span>InputStream inputStream = conn.getInputStream();
<span style="white-space:pre">					</span>bitmap = BitmapFactory.decodeStream(inputStream);
<span style="white-space:pre">					</span>CommonUtil.saveUserPicToFile(context, bitmap, uri);
<span style="white-space:pre">					</span>inputStream.close();
<span style="white-space:pre">				</span>} catch (Exception e) {
<span style="white-space:pre">					</span>e.printStackTrace();
<span style="white-space:pre">				</span>}
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}.start();
<span style="white-space:pre">	</span>}

                http://sishuok.com/forum/blogPost/list/5023.html


注意: 快捷方式如果是生成点击直接进入网页,点击的时候,显示未安装应用,一般是网址输入错误

         快捷方式,点击进入指定的某个页面的时候出现一个对话框,一般是你指定的路径不正确,或者指定的某个activity不存在

对应跳转的Activity的清单文件配置:

<activity android:name="com.fly.update.activity.NotificationUpdateActivity" 
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="com.example.updata.NotificationUpdateActivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值