【问题标题】:Programatically change icon resource on click单击时以编程方式更改图标资源
【发布时间】:2014-01-02 19:53:24
【问题描述】:

我正在尝试更改 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();

}

有什么想法吗?

【问题讨论】:

    标签: android android-intent icons shortcut


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多