Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android獲取應用程序名稱(ApplicationName)

Android獲取應用程序名稱(ApplicationName)

編輯:關於Android編程

MainActivity如下:

[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描述:
 * 獲取應用程序名稱(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; 
    } 
 

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如下:

[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>

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved