Search code examples
androidandroid-intentandroid-activityandroid-package-managersandroid-application-class

getExtras() return null on Application level (-class)


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?


Solution

  • 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.