I am developing an Bus-Timetable Android Application and I want that users can make a shortcut with their favourite bus stop.

When a user clicks on the shortcut the departure times of this bus stop should appear.

I already know how to create a shortcut but dont know how to save data to that (in my app the bus stop ID).

有帮助吗?

解决方案

found the solution quite easily

Intent shortcutIntent = new Intent(context.getApplicationContext(),MainActivity.class);
shortcutIntent.putExtra("stop", stopName);
shortcutIntent.setAction(Intent.ACTION_MAIN);

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

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

get the extra "stop" on ActivityMain.java ...

Intent i = getIntent();
String stop = i.getStringExtra("stop");

其他提示

Why dont use SharedPreference?

this is how u save data, simple int:

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("your_int_key", yourIntValue);
editor.commit();

this is how you get data:

SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
 int myIntValue = sp.getInt("your_int_key", -1);

if you look is simple or maybe you need sqlite to save information but is more complex to resolve here in one post.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top