Вопрос

I am very new to android and I am having trouble getting this down. I have been succesfully able to create a shortcut from within my app. The only problem is that I cannot dictate what happenes after the shortcut is clicked. It just launches my MainActivity, but I want it to also pass it data when the main activity is selected. Here is what I have...

Intent shortcutIntent = new Intent(getActivity().getApplicationContext(),
            MainActivity.class);

    shortcutIntent.setAction(Intent.ACTION_MAIN);

    Intent addIntent = new Intent();
    addIntent
            .putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, f.getName());
    addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
            Intent.ShortcutIconResource.fromContext(getActivity().getApplicationContext(),
                    R.drawable.icon_folderbluegray));
    addIntent.putExtra("info for Main Activity","Hello");

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

But when I do this, I get null...

Bundle extras = getIntent().getExtras();

Here is what I have in the Mainfest.

             <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT"/>
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>
Это было полезно?

Решение

what you are doing is sendigBroadcast.... thats why you are getting null

getActivity().getApplicationContext().sendBroadcast(addIntent);

that's why you get null in the bundle here

Bundle extras = getIntent().getExtras();

To handle the result you need to implement a broadcast receiver

EDIT IMPLEMENT THE RECEIVER

public final BroadcastReceiver noteCompletedReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(NoteListActivity.this, "Broadcast received",   Toast.LENGTH_LONG).show();
       //HANDLE HERE THE INTENT
    }
};

also you need to set a intentFilter, you can register dynamic in your onCreate method like

IntentFilter filter = new IntentFilter(=====SOME CONSTANT THAT YOU DEFINE AND THAS PASSED FROM THE STARTING INTENT====);
registerReceiver(noteCompletedReceiver, filter);

and in onDestroy method of your activity you need to call

unregisterReceiver(noteCompletedReceiver);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top