android,如何获取包名? [英] android, how to get package name?

查看:1250
本文介绍了android,如何获取包名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要知道包名称的名称。我没有问题,当我想抓住它在活动,但我不能把它在其他类。以下代码在活动中工作,但我不知道为什么它在简单类中有问题。

  String packageName = getPackageName ; 

在我的班里,我试着写下这段代码:

 上下文上下文= getApplicationContext(); 
String packageName = context.getPackageName();

但编译器说 getApplicationContext()

解决方案



div>

简单或另一种方式是将Context传递到辅助类的构造函数中:

  MyClassConstructor {

String packageName = context.getPackageName();
}


In my application I need to know the name of package name. I have no problem when I want to grab it in activities but i can't take it in other classes. Following code is working in activity but i don't know why it has problem in simple class.

String packageName = getPackageName();

In my class I tried to write this code:

Context context = getApplicationContext();
String packageName = context.getPackageName();

but compiler said getApplicationContext() method is undefined for this class.

How can I take package name within this class?

解决方案

The simple, or another way is to pass Context into the helper class constructor:

MyClassConstructor(Context context){

        String packageName = context.getPackageName(); 
}

这篇关于android,如何获取包名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆