问题描述:

I've read somewhere (but I can't find it anymore) that it should be possible to send extras from a shortcut on the device home screen. I created successfully a shortcut, but Bundle extras = getIntent().getExtras(); gives a nullpointer.

I create the shortcut as follows:

 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);

Is it possible? And ifso, how?

网友答案:

Yes, I have achieved it, Here is the code

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);
    }
网友答案:

As found here: http://www.joulespersecond.com/2010/04/android-tip-effective-intents/

Fortunately, there’s a solution. The better way is to put the row ID as part of a URI, rather than as a part of an extra. So the previous code becomes something like this:

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();
}

BASE_URI can be anything, but it should be something that’s specific to your application. The point is that the Data URI is used in determining whether or not two intents are equal, so the system will end up creating a new Activity for this, even if the same Activity with different data is on your task’s stack.

相关阅读:
Top