3

On my Application level I receive null for getExtras(), but on Activity level i can see them correctly.

public class MyApplication extends Application 
{
    @Override
    public void onCreate() {
        super.onCreate();
        Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.MyApp");
        if (intent != null){
            String mStaticWorldUrl = intent.getStringExtra("arg1Name");
            String mStaticWorldIconUrl = intent.getStringExtra("arg2Name");
            Log.i("LOG", mStaticWorldUrl + " ---  " + mStaticWorldIconUrl);
        }
    }
}

I'm calling the app from some shortcuts that were created by this code:
(- each shortcut has different Extras sent to the Intent)

    // create a shortcut for the specific app
public static void createShortcutForPackage(Context context,
        String packageName, String className, String shortcutName,
        String arg1Name, String arg1Val, String arg2Name, String arg2Val,
        int iconID) {

    Intent intent = new Intent();
    intent.setComponent(new ComponentName(packageName, className));

    PackageManager pm = context.getPackageManager();

    Context pkgContext = createPackageContext(context, packageName);
    if (pkgContext == null)
        return;

    Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
    Intent shortcutIntent = pm.getLaunchIntentForPackage(packageName);

    if (arg1Name != null)
        shortcutIntent.putExtra(arg1Name, arg1Val);

    if (arg2Name != null)
        shortcutIntent.putExtra(arg2Name, arg2Val);

    shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, shortcutName);
    shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(context, iconID));
    shortcut.putExtra("duplicate", false);
    context.sendBroadcast(shortcut);
}  

how can I read these Extras on the Application level?
or is there any other way to create different shortcuts for application and read its' "parameters" data on Application?

RedHat
  • 167
  • 10

2 Answers2

4

The Application class is static for the application: there is only ever a single instance of it for your app's process. If your app has been launched with a normal launch Intent, rather than a shortcut you created, then no extras would be present. The app process does not die when HOME or BACK is pressed, so the Intent used to launch the package may not be what you think it should be.

You should not need to look at the Intent at the Application level. Intent objects are not intended to be "sent" there, but rather to an Activity, Service or BroadcastReceiver.

Larry Schiefer
  • 15,325
  • 1
  • 25
  • 32
  • 1
    The reason that @RedHat got no extras is because the queried intent was one that was generated one line earlier, it is the intent that the OS has generated for the package as requested in the following code: Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.MyApp"); – yakobom Aug 01 '16 at 12:50
  • Yes, that is correct: the `PackageManager` method is returning an `Intent` which *can be used to launch the specified package*, which is different than the actual `Intent` used to launch a component of the package (which is what @RedHat desired.) But, my answer is still valid: you don't want to look at `Intent` objects within an `Application` class instance: they are not delivered there. – Larry Schiefer Aug 01 '16 at 13:15
  • Yes @LarrySchiefer, you are correct of course. It was important, however, to point out the other mistake since it is a conceptual error. Moving the same code to the Activity level would also lead to the same problem. – yakobom Aug 02 '16 at 04:05
1

This is conceptual error which facing to get data in application class using getExtra which is the method of Intent

Reason for this issue:

  • No extras is because the queried intent was one that was generated one line earlier, it is the intent that the OS has generated for the package as requested in the following code: Intent intent = getPackageManager().getLaunchIntentForPackage("com.example.M‌​yApp");

  • Intent objects within an Application class instance: they are not
    delivered there

Let's understand following things to use in upcoming usage while anyone want to get data in application level

  • What is Intent ?
  • What is the Use of Intent?
  • What other things can we use to achieve this?

What is Intent?

  • An Intent provides a facility for performing Late runtime binding between the code in different applications . Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

  • extras - This is a Bundle of any additional information. This can be used to provide extended information to the component. For example, if we have a action to send an e-mail message, we could also include extra pieces of data here to supply a subject, body, etc.

What is the Use of Intent?

Use to intents facilitate communication between components in several ways, Followings are standard use

  • To start an activity.
  • To start a service.
  • To deliver a broadcast

What other things can we use to achieve this?

  • There are lots of things we can use to achive this and solved this issue.

  • But right now here i mentioned only one which is standard and secured to use application

  • Content provider: To offer a file from your app to another app is to send the receiving app the file's content URI and grant temporary access permissions to that URI. Content URIs with temporary URI access permissions are secure because they apply only to the app that receives the URI, and they expire automatically.

Patrick R
  • 5,704
  • 1
  • 18
  • 25