最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【未解决】android项目中代码中如何获得对应的项目名

Android crifan 2575浏览 0评论

【背景】

希望在代码中输出log。

之前已经实现了log的功能:

【记录】给Android中添加log日志输出到文件

但是默认输出的位置是

sd卡的根目录

导致:

程序运行多次后,在sd卡根目录下生成了太多的log文件,很乱。

希望:

修改log部分代码,使得生成的log文件,都只放在某个文件夹中。

该文件夹,此处打算用项目名称。

所以需要代码中获得项目名。

【折腾过程】

1.参考:

【记录】android中获得res/xml下面的关于USB过滤配置的资源文件信息

去试试:

getResources().

结果由于不是context所以无法使用该函数。

2.参考:

How to get package name from anywhere?

但是没看到合适的。

3.参考:

Android: Get application name (not package name)

get application name from package name

都是获得app的名字,而不是此处的项目名。

4.期间,又遇到对应的:

【未解决】android中如何拼接路径

5.结果用:

import com.xx.abcde.Start;

public class Start extends Application {
	private static Start instance;
	
	public static Start getInstance() {
	    return instance;
	}
	
    public static Context getContext(){
        return instance;
        // or return instance.getApplicationContext();
    }
    
    //...
}

String projectName = Start.getContext().getPackageName();

只能获得:

包的名字:com.xx.abcde

而不是我所希望的:

项目的名字:ABCDE

6.继续去改代码:

String projectName = Start.getContext().getString(Start.getContext().getApplicationInfo().labelRes);

试试效果,结果得到的是app的名字,是中文。

不是我要的英文的项目名称。

7.先用代码:

		Context curContext = Start.getContext();
		ApplicationInfo appInfo = curContext.getApplicationInfo();
		String projectName = appInfo.className;

去调试看看,运行时的变量中信息都是哪些,看看能否找到我要的那个label。

结果在运行时中的ApplicationInfo中,没有找到我要的:

runtime application info

算了,截止目前,还是没有完美的直接获得对应的app_name。

8.参考:

How to get the name of the application in android?

去试试,

		Context curContext = Start.getContext();
		Resources appR = curContext.getResources();
		CharSequence appNameCharSeq = appR.getText(appR.getIdentifier("app_name", "string", curContext.getPackageName()));		
		String projectName = appNameCharSeq.toString();

得到的结果,和之前

String projectName = Start.getContext().getString(Start.getContext().getApplicationInfo().labelRes);

效果是一样的。。。

还是那个中文,是app的name。

而不是我要的英文。

9.参考:

How to get the application name of the APK programmatically (not installed)

去试试那个getApplicationLabel

		Context curContext = Start.getContext();
		ApplicationInfo appInfo = curContext.getApplicationInfo();
		PackageManager packageManager = curContext.getPackageManager();
		Resources appR = curContext.getResources();
		
		CharSequence appNameCharSeq = appR.getText(appR.getIdentifier("app_name", "string", curContext.getPackageName()));
		String appLabel = packageManager.getApplicationLabel(appInfo).toString();

结果和上面一样:

得到的是中文的app的name。

10.下面的:

		Context curContext = Start.getContext();
		ApplicationInfo appInfo = curContext.getApplicationInfo();
		PackageManager packageManager = curContext.getPackageManager();
		Resources appRes = curContext.getResources();
		
		CharSequence appNameCharSeq = appRes.getText(appRes.getIdentifier("app_name", "string", curContext.getPackageName()));
		String appLabel = packageManager.getApplicationLabel(appInfo).toString();
		String appName = appRes.getString(R.string.app_name);
				
		String projectName = appNameCharSeq.toString();

效果还是依旧得到那个中文的app的name。

10.截止目前:

android的项目的:

AndroidManifest.xml

中定义的:

<application
    android:name="com.xx.yy.Start"
    android:label="@string/app_name"

    <activity
        android:name="com.xx.yy.activities.DeviceListActivity"
        android:label="@string/app_name"

结果:

程序运行时,之前的代码,始终都只是:

要么是获得的是application的android:name的值;

要是获得的是activity的android:label的值,就没法获得:

application的android:label的值。

 

【总结】

算了,截止目前,还是没有获得对应的android的项目的AndroidManifest.xml中的application中所定义的那个android:label的值。。。

转载请注明:在路上 » 【未解决】android项目中代码中如何获得对应的项目名

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.170 seconds, using 22.15MB memory