Android 快捷主屏幕,如何发送附加内容

Android 快捷主屏幕,如何发送附加内容,android,android-intent,shortcut,Android,Android Intent,Shortcut,我在某个地方读到(但我再也找不到了)应该可以从设备主屏幕上的快捷方式发送额外内容。我成功创建了一个快捷方式,但是Bundle extras=getIntent().getExtras()提供一个空指针 我创建快捷方式如下所示: Intent shortcutIntent = new Intent(this.getApplicationContext(), Shortcut_Activity.class); s

我在某个地方读到(但我再也找不到了)应该可以从设备主屏幕上的快捷方式发送额外内容。我成功创建了一个快捷方式,但是
Bundle extras=getIntent().getExtras()提供一个空指针

我创建快捷方式如下所示:

   Intent shortcutIntent = new Intent(this.getApplicationContext(),
                        Shortcut_Activity.class);

                shortcutIntent.setAction(Intent.ACTION_MAIN);

                Intent addIntent = new Intent();
                addIntent
                        .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
                addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
                        Intent.ShortcutIconResource.fromContext(this.getApplicationContext(),
                                R.drawable.ic_shortcut));
                addIntent.putExtra("ID", id); //THIS IS THE EXTRA DATA I WANT TO ATTACH
                addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
                this.getApplicationContext().sendBroadcast(addIntent);
可能吗?如果是这样,怎么做?

如下所示:

幸运的是,有一个解决方案。更好的方法是将行ID作为URI的一部分,而不是作为额外URI的一部分。所以前面的代码是这样的:

void returnShortcut(int rowId, String shortcutName) {
    Intent i = new Intent(this, ShowInfoActivity.class);
    i.setData(ContentUris.withAppendedId(BASE_URI, rowId));
    Intent shortcut = new Intent();
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
    setResult(RESULT_OK, shortcut);
    finish();
}

基本URI可以是任何内容,但它应该是特定于应用程序的内容。关键是,数据URI用于确定两个意图是否相等,因此系统最终将为此创建一个新的活动,即使任务堆栈上存在具有不同数据的相同活动。

是的,我已经实现了,下面是代码

private void addShortcut() {
    //Adding shortcut for MainActivity 
    //on Home screen
    Intent shortcutIntent = new Intent(getApplicationContext(),
            HOMESHORTCUT.class);
    //Set Extra
    shortcutIntent.putExtra("extra", "shortCutTest ");

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "HelloWorldShortcut");
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getApplicationContext(),
                    R.drawable.ic_launcher));

    addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");

    getApplicationContext().sendBroadcast(addIntent);


}
//GET Extra in HOMESHORTCUT activity.
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTextExtra=new TextView(this);
        Bundle bundel=getIntent().getExtras();
        String getExString=getIntent().getExtras().getString("extra");
        mTextExtra.setText(getExString);
        setContentView(mTextExtra);
    }

但是,如果包(此处命名为extras)(无论出于何种原因)为null,则此代码将崩溃。如果您仍有null异常,请删除旧的快捷方式,并在使用
addIntent.putExtra(“复制”,false)时创建新的快捷方式