• 0

[JAVA/Android] how to get package info from within static context?


Question

I'm trying to create an "About" window that pops up from my app when a user clicks "About app_name" in my preferences screen. It all works, but I'd like to have the version number get pulled from the packagemanager, but can't seem to be able to do it inside a static routine:

public class AboutDialogBuilder {


	public static AlertDialog create( Context context ) throws NameNotFoundException {

                //get info about package so we can get version number out of it.
		PackageManager manager = context.getPackageManager();
        	PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);


		String aboutTitle = String.format("About %s", context.getString(R.string.app_name));


		// Set up the TextView
		final TextView message = new TextView(context);
		// We'll use a spannablestring to be able to make links clickable
//		final SpannableString s = new SpannableString(aboutText);
		message.setText("Version: 1.0" + "\n\n" + "icon sets by http://www.iconshock.com");
		//message.setText(string.iconshock);
		// Set some padding
		message.setPadding(5, 5, 5, 5);
		message.setBackgroundColor(0xFFE3E3E3);
		message.setTextColor(0xFF000000);
		// Set up the final string


		// Now linkify the text
		Linkify.addLinks(message, Linkify.WEB_URLS);

		return new AlertDialog.Builder(context).setTitle(aboutTitle).setCancelable(true).setIcon(R.drawable.app_icon).setPositiveButton(
			 context.getString(android.R.string.ok), null).setView(message).create();
	}
}

The two lines related to packagemanager at the top are what I'm having issues with. I get a message in Eclipse stating "Cannot use this in a static context", with "this" being underlined in my code. I currently have the line "message.setText(.....)" to display the Version number, but I'd like to get it pulled from the package instead so that I don't have to continually update the code if I update the app.

Thanks.

2 answers to this question

Recommended Posts

  • 0

I had to refresh real quick on the 'this' reference you're using first:

  Quote

Whenever a method is called in C++/Java/C#, an implicit argument (the ?this? reference) is passed along with/without the other parameters. In case of a static method call, the ?this? reference is not passed as static methods belong to a class and hence do not have the ?this? reference.

I found this snippet that seems to get a Package object p and extracts the version without the need for a 'this' ref.

public class GetPackageInfo {
    public static void main(String[] args) {
	packageInfoDemo("java.util.zip");
    }

    public static void packageInfoDemo(String pkgName) {
	Package p = Package.getPackage(pkgName);
	System.out.println("Specificationion: " + p.getSpecificationVersion());
	System.out.println("Specificatione: " + p.getSpecificationTitle());
	System.out.println("Specificationor: " + p.getSpecificationVendor());
    }
}

Then I found this other snippet that lacks your 'context' reference but still seems to get at the PackageInfo object like you want. Might help but I haven't used this class before (can you tell? :p ) The code snippet below comes from here and has a more detailed example with class code.

public void getVersion(View view) {
	CharSequence name = nameField.getText();
	try {
	    PackageInfo pkgInfo = getPackageManager().getPackageInfo(name.toString(), 0);
	    versionInfoField.setText(String.format(
		getString(R.string.packageInfo),
		pkgInfo.versionCode, 
		pkgInfo.versionName
	    ));
	}
	catch (PackageManager.NameNotFoundException e) {
	    versionInfoField.setText(getString(R.string.packageNotFound));
	}
    }

  • 0

You cannot use 'this' in a static method because there is no instance of the class to refer to there.

It just requires the package Name, so you could manually enter that. Or do something like AboutDialogBuilder.class.getPackage().getName()

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • If you have top secret documents from the government or your company, you will not use a user cloud service. You will use a cloud service aimed at companies. Even governments use cloud services made for governments. Stop being so stupid that you think that your useless information on your PC is as important as you think it is. Your information is not important at all, you are not important to the government or to Microsoft, Google or Apple. If you cared about the content, you would not use these documents and photos on Android and iPhone phones because the telemetry there is 100,000% worse than on Windows 11.
    • "not some corporate backdoor deal." Sure. In the last years, Nexus Mod has adopted a lot of corporate-ish politics, including DEI. Nobody (of the users of the platform) asked for it.
    • So, user error then. If there aren't at least 2 copies of a dataset, that data isn't important. Literally in the ToS You should have a regular backup plan as Microsoft won’t be able to retrieve Your Content or Data once your account is closed. Also this article is mixing bitlocker in - it isn't in play here at all that I can see. So the guy uploaded a load of data, then destroyed the source data, then 'lost' access to the MSA. Why would you do the upload and then blow the source away, makes no sense.
    • Cloud is a security risk. You are sharing information with a third party, and this third party could (and might) share your information with others (for statistics, analysis, etc., a risk of security). And this third party could delete or restrict access to your information at will.
    • Well, apparently those won't all be CPU lanes, only 24 of them. And instead of an 8-lane gen 4 link to the chipset, they'll revert back to 4 lanes, but at gen 5 speeds. That means that there's barely any improvement in I/O, which is disappointing. The only improvement that I see is that the 4 lanes that were gen 4 on the CPU become gen 5 too. Some lanes become gen 5 on the chipset, but that only makes it easier to hit a bottleneck on the CPU-chipset link.
  • Recent Achievements

    • Experienced
      dismuter went up a rank
      Experienced
    • One Month Later
      mevinyavin earned a badge
      One Month Later
    • Week One Done
      rozermack875 earned a badge
      Week One Done
    • Week One Done
      oneworldtechnologies earned a badge
      Week One Done
    • Veteran
      matthiew went up a rank
      Veteran
  • Popular Contributors

    1. 1
      +primortal
      684
    2. 2
      ATLien_0
      266
    3. 3
      Michael Scrip
      196
    4. 4
      +FloatingFatMan
      181
    5. 5
      Steven P.
      142
  • Tell a friend

    Love Neowin? Tell a friend!