以编程方式更改单击时的图标资源

时间:2014-01-02 19:53:25

标签: android android-intent icons shortcut

我正在尝试更改Intent.EXTRA_SHORTCUT_ICON_RESOURCE的来源

public class ShortcutActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // The meat of our shortcut
    final Intent shortcutIntent = new Intent(this, ShortcutResult.class);
    ShortcutIconResource iconResource = Intent.ShortcutIconResource
            .fromContext(this, R.drawable.test);
    // The result we are passing back from this activity
    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Test");
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);

    setResult(RESULT_OK, intent);

    finish(); // Must call finish for result to be returned immediately
}

我访问另一个启动服务的活动。当服务开始和停止时,我希望能够在两个图像之间交替。

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sharedPrefs = getApplicationContext().getSharedPreferences("eePrefs2",
            Context.MODE_PRIVATE);
    editor = sharedPrefs.edit();

    if (sharedPrefs.getBoolean("service_running", false) == false) {
        startService(new Intent(this, service.class));
        editor.putBoolean("service_running", true);
        ShortcutIconResource iconResource = Intent.ShortcutIconResource
                .fromContext(this, R.drawable.test);
    } else if (sharedPrefs.getBoolean("service_running", false) == true) {
        stopService(new Intent(this, service.class));
        editor.putBoolean("service_running", false);
        ShortcutIconResource iconResource = Intent.ShortcutIconResource
                .fromContext(this, R.drawable.test2);
    }
    Intent intent = new Intent();
    intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
    setResult(RESULT_OK, intent);
    editor.commit();
    finish();

}

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

这是不可能的。没有协议可以告诉任意主屏幕更改快捷方式图标。

相反,您可以创建一个应用小部件,它可以让您更好地控制演示文稿。

答案 1 :(得分:0)

我认为ShortcutIconResource不是你想要的。您是否在服务运行时尝试更改ActionBar中的图标?或者您是否在服务运行时尝试更改整个应用程序的图标?

相关问题