MainActivity is as follows:
[Java]
Package cn. testapplicationname;
Import android. OS. Bundle;
Import android. widget. TextView;
Import android. app. Activity;
Import android. content. pm. ApplicationInfo;
Import android. content. pm. PackageManager;
/**
* Demo description:
* Obtain the application name (ApplicationName)
*/
Public class MainActivity extends Activity {
Private TextView mTextView;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
MTextView = (TextView) findViewById (R. id. textView );
String applicationName = getApplicationName ();
MTextView. setText ("Name of the application:" + applicationName );
}
Public String getApplicationName (){
PackageManager packageManager = null;
ApplicationInfo applicationInfo = null;
Try {
PackageManager = getApplicationContext (). getPackageManager ();
ApplicationInfo = packageManager. getApplicationInfo (getPackageName (), 0 );
} Catch (PackageManager. NameNotFoundException e ){
ApplicationInfo = null;
}
String applicationName =
(String) packageManager. getApplicationLabel (applicationInfo );
Return applicationName;
}
}
Package cn. testapplicationname;
Import android. OS. Bundle;
Import android. widget. TextView;
Import android. app. Activity;
Import android. content. pm. ApplicationInfo;
Import android. content. pm. PackageManager;
/**
* Demo description:
* Obtain the application name (ApplicationName)
*/
Public class MainActivity extends Activity {
Private TextView mTextView;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Init ();
}
Private void init (){
MTextView = (TextView) findViewById (R. id. textView );
String applicationName = getApplicationName ();
MTextView. setText ("Name of the application:" + applicationName );
}
Public String getApplicationName (){
PackageManager packageManager = null;
ApplicationInfo applicationInfo = null;
Try {
PackageManager = getApplicationContext (). getPackageManager ();
ApplicationInfo = packageManager. getApplicationInfo (getPackageName (), 0 );
} Catch (PackageManager. NameNotFoundException e ){
ApplicationInfo = null;
}
String applicationName =
(String) packageManager. getApplicationLabel (applicationInfo );
Return applicationName;
}
}
Main. xml is as follows:
[Html]
<RelativeLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
>
<TextView
Android: id = "@ + id/textView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"
Android: layout_centerInParent = "true"
/>
</RelativeLayout>
<RelativeLayout
Xmlns: android = "http://schemas.android.com/apk/res/android"
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
>
<TextView
Android: id = "@ + id/textView"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello_world"
Android: layout_centerInParent = "true"
/>
</RelativeLayout>