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.
Question
SirEvan
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:
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
https://www.neowin.net/forum/topic/1026088-javaandroid-how-to-get-package-info-from-within-static-context/Share on other sites
2 answers to this question
Recommended Posts