未显示Android主屏幕快捷方式图标

时间:2011-10-14 15:09:08

标签: android icons homescreen

我达到了一个级别,我将通过代码添加一个快捷方式到android的主屏幕。我从远程服务器下载图标,然后将此图标设置为主屏幕的快捷方式。

以下是代码:

String url = "http://www.the-brights.net/images/icons/brights_icon_50x50.gif";
    InputStream ins = null;

    try {
        ins = new java.net.URL(url).openStream();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    Bitmap bm = BitmapFactory.decodeStream(new FlushedInputStream(ins));
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    OutputStream outStream = null;

    try {
        outStream = mContext.openFileOutput("test.png", Context.MODE_PRIVATE);
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }


    Log.i("info", "Got here");
    Bitmap bm2;
    try {
        bm2 = BitmapFactory.decodeStream(mContext.openFileInput("test.png"));
        Log.i("info", bm2.toString());

        Intent shortcutIntent = new Intent();
        shortcutIntent.setClassName(mContext, mContext.getClass().getName());
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        shortcutIntent.putExtra("someParameter", "HelloWorld 123");
        Intent addIntent = new Intent();
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Name 123");
        addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, bm2);
        addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
        mContext.sendBroadcast(addIntent); 
    }
    catch(Exception e) {
        e.printStackTrace();
    }

问题是正在添加快捷方式,但图标没有出现,显示默认快捷方式图标......我在这里做错了什么?

1 个答案:

答案 0 :(得分:3)

使用EXTRA_SHORTCUT_ICON代替EXTRA_SHORTCUT_ICON_RESOURCE,问题将解决