简体   繁体   中英

How do I read the label text from the shortcut that invoked my Android App?

Can I read the label text from the application shortcut that invoked my Android program?

I can read the application name:

getString(R.string.app_name)

but that's not what i want. It needs to be the custom label text that is assigned to the shortcut.

I plan to have multiple shortcuts with different labels. The label will influence some behaviour of my program.

不,启动快捷方式的标签不会以任何方式传递给您。

You don't necessarily need to know the name or text of the shortcut you made, instead you can use that shortcut to "pass arguments" to the activity via an intent. Your shortcut will be a regular Activity, it will just pass an argument to the main Activity of your application using an Intent.

Intent i = new Intent(this, SomeActivity.class);
i.putExtra("some_tag", true);
startActivity(i);

And in the Activity to be launched,

@Override
protected void onNewIntent(Intent in) {
    super.onNewIntent(in);

    if(in.hasExtra("some_tag") && in.getExtra("some_tag") == true) {
        //Do Something Special
    }
}

Note that you need launchMode set to singleTop to use onNewIntent() as noted here: http://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent)

More information can be found here:

http://www.basic4ppc.com/forum/basic4android-getting-started-tutorials/11444-add-shortcuts-your-android-application.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM