问题
What I want to do is to pass a certain key value to the intent indicate the app that it was open trough shortcut, so it does certain task instead of standard launch. In my case the app needs to download data from the server before it continues. I want the shortcut to be available after install so I cannot put a dynamic shortcut as I did so far after launch. I also tried to do this by opening a special activity where I put the key to the intent, but I would need to do this for every shortcut separately because I don't know ho to determine which shortcut the user tap. How can I put primitive data to the intent in the shortcut.xml ?
EDIT_1: Would that be trough categories? Maybe one way of doing it.
EDIT_2 current solution: I solved it by putting category in the shortcut intent in the xml file. How ever I am still looking for a namespace for putting primitive data into xml intent.
回答1:
U should probablly check android documentation and have a look to how to add a Dynamic ShortCut check this out, u can pass a intent this way, using a Bundle perhaps Dynamic ShortCut App
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
ShortcutInfo shortcut = new ShortcutInfo.Builder(this, "id1")
.setShortLabel("Web site")
.setLongLabel("Open the web site")
.setIcon(Icon.createWithResource(context, R.drawable.icon_website))
//check out this part
.setIntent(new Intent(Intent.ACTION_VIEW,
Uri.parse("https://www.mysite.example.com/")))
.build();
shortcutManager.setDynamicShortcuts(Arrays.asList(shortcut));
For Static Shorcut if u want do detect something an action from what shorcut u pressed use this under ur xml shorcut > intent :
<intent
android:action="com.example.whatever.WTF"
..../>
Then when in your activity declare a constant.
private static final String ACTION_ADD_WTF ="com.example.whatever.WTF";
Then while onCreate() put this:
if (ACTION_ADD_WTF.equals(getIntent().getAction())) {
// Invoked via the manifest shortcut.
//TODO:put your logic here
}
P.D WTF in Android is What a Terrible Failure, dont think bad! :D
回答2:
If you want to set the intent's data uri, you can do it by setting it in the static xml (https://developer.android.com/guide/topics/ui/shortcuts.html#static):
...
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.yourname.yourapp"
android:targetClass="com.yourname.yourapp.activities.ShortcutActivity"
android:data="content://com.yourname.yourapp/compose/new?from=shortcut" />
...
In your activity, you get the data uri back:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Uri data = this.getIntent().getData();
}
来源:https://stackoverflow.com/questions/42554349/how-to-add-data-to-app-shortcut-intent