关于具体使用方法,举例如下:
1、updatelog.xml文件位于res/xml/文件夹下:
versioncode="1.1">
2、在代码中调用方法如下:
public class ResourcesActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView( R.layout.resources_layout );
mTextView = ( TextView )findViewById( R.id.textId );
getFileResources( );
}
private void getFileResources( ){
String packageName = getPackageName( );
try {
Resources resources = this.getPackageManager( ).getResourcesForApplication( packageName );
int resId = resources.getIdentifier( "updatelog", "xml", packageName );
String log = getUpdatelog( resId, resources );
int appId = resources.getIdentifier( "action_settings", "string", packageName );
String appName = resources.getString( appId );
mTextView.setText( appName + "\n" + log );
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
private String parseTag( XmlResourceParser xml ) throws XmlPullParserException, IOException {
String result = "Version: "+ xml.getAttributeValue(null, "version") + "\n" + "VersionCode: " + xml.getAttributeValue(null, "versioncode") + "\n";
int eventType = xml.getEventType( );
while( ( eventType != XmlPullParser.END_TAG ) || ( xml.getName( ).equals( "log" ) ) ) {
if( (eventType == XmlPullParser.START_TAG) && ( xml.getName( ).equals( "log" ) ) ) {
eventType = xml.next( );
result = result + xml.getText( ) + "\n";
}
eventType = xml.next( );
}
return result;
}
private String getUpdatelog( int resourceId, Resources resource ) {
String result = "";
XmlResourceParser xmlResParser = resource.getXml( resourceId );
try {
int eventType = xmlResParser.getEventType( );
while ( eventType != XmlPullParser.END_DOCUMENT ) {
if ( ( eventType == XmlPullParser.START_TAG ) && ( xmlResParser.getName( ).equals( "release" ) ) ) {
result = result + parseTag( xmlResParser );
}
eventType = xmlResParser.next( );
}
} catch (XmlPullParserException e) {
System.out.println( e.getMessage( ) );
} catch (IOException e) {
System.out.println( e.getMessage( ) );
} finally {
xmlResParser.close( );
}
return result;
}
private TextView mTextView = null;
}