Android获取应用程序名称(ApplicationName)示例
程序员文章站
2023-01-02 08:55:01
mainactivity如下: 复制代码 代码如下: package cn.testapplicationname; import android.os.bundle; i...
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如下:
<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>
复制代码 代码如下:
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如下:
复制代码 代码如下:
<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>