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

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

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

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("Specification Version: " + p.getSpecificationVersion());
	System.out.println("Specification Title: " + p.getSpecificationTitle());
	System.out.println("Specification Vendor: " + 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));
	}
    }

Link to comment
Share on other sites

  • 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()

Link to comment
Share on other sites

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

    • No registered users viewing this page.