当前位置: 编程技术>移动开发
Android获取应用程序名称(ApplicationName)示例
来源: 互联网 发布时间:2014-10-20
本文导语: MainActivity如下: 代码如下: 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描述: * 获取应用程序名称(ApplicationNa...
MainActivity如下:
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描述:
* 获取应用程序名称(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("该应用名字:"+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如下:
代码如下:
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描述:
* 获取应用程序名称(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("该应用名字:"+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如下:
代码如下: