【问题标题】:How to get application name in android?如何在android中获取应用程序名称?
【发布时间】:2017-01-19 11:48:30
【问题描述】:

当应用程序安装在设备中时以及成功后。我必须获取该应用程序包名称,并且从包名称中我必须识别“应用程序名称”。我该怎么做呢

代码:-

private String TAG = CAppReceiver.class.getSimpleName();
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (action.equals("android.intent.action.PACKAGE_ADDED")){
        Log.d(TAG,"App package::" + intent.getData().toString());
    }
}

【问题讨论】:

标签: android


【解决方案1】:
 String pakageName =getPackageName();
 String[] separated = pakageName.split(".");
 separated[0]; 
 separated[1];  

听到获取应用程序名称的代码,首先获取包名称,然后将其拆分以获取应用程序名称。

【讨论】:

    【解决方案2】:

    在您的清单中,添加具有相关意图服务的接收器(在应用程序标签内):

    <receiver android:name=".apps.AppListener">
        <intent-filter android:priority="100">
             <action android:name="android.intent.action.PACKAGE_INSTALL"/>
             <data android:scheme="package"/> 
        </intent-filter>
    </receiver>
    

    根据谷歌文档:

    PACKAGE_INSTALL 广播动作:一个新的 应用程序包已安装在设备上。数据 包含包的名称。注意新安装的 包没有收到这个广播。

    然后使用您的 onReceive 代码方法。

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.util.Log;
    
    public class AppListener extends BroadcastReceiver {
    
    @Override
    public void onReceive(Context context, Intent arg1) {
        // TODO Auto-generated method stub
        Log.v(TAG, "there is a broadcast");
        }
    }
    

    【讨论】:

      【解决方案3】:

      使用以下方法获取应用程序包名。

      intent.getData().getSchemeSpecificPart()
      

      并使用以下代码获取其他应用程序特定信息。

       try {
          ApplicationInfo app = this.getPackageManager().getApplicationInfo("com.example.name", 0);        
      
          Drawable icon = packageManager.getApplicationIcon(app);
          String name = packageManager.getApplicationLabel(app);
          return icon;
      } catch (NameNotFoundException e) {
          Toast toast = Toast.makeText(this, "error in getting icon", Toast.LENGTH_SHORT);
          toast.show();
          e.printStackTrace();
      }
      

      【讨论】:

        【解决方案4】:

        如果您有包名称,则可以获取您的应用名称或应用标签。方法如下

        PackageManager packageManager= getApplicationContext().getPackageManager();
        String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo("YourPackageName", PackageManager.GET_META_DATA));
        

        【讨论】:

          猜你喜欢
          • 2017-04-24
          • 1970-01-01
          • 2012-02-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-13
          • 1970-01-01
          • 2011-08-21
          相关资源
          最近更新 更多