[英]Telephony manager in Application class
When I try to use telephony manager to retrieve the phone number from an Activity class, I am able to do it successfully. 当我尝试使用电话管理器从Activity类中检索电话号码时,我能够成功完成此操作。 But I will be using the phone number in multiple places of the app, therefore I shifted the phone number to be a static field in my application class.
但是我将在应用程序的多个位置使用电话号码,因此我将电话号码更改为应用程序类中的静态字段。
public class FourApplication extends Application {
static String phonenumber ;
@Override
public void onCreate() {
super.onCreate();
ParseObject.registerSubclass(Post.class);
// Add your initialization code here
Parse.initialize(this, "**********", "*********");
ParseACL defaultACL = new ParseACL();
// If you would like all objects to be private by default, remove this
// line.
defaultACL.setPublicReadAccess(true);
ParseACL.setDefaultACL(defaultACL, true);
phonenumber = getPhoneNumber();
}
public String getPhoneNumber()
{
TelephonyManager tMgr =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number(); ;
Log.i("mPhoneNumber : ", mPhoneNumber);
return mPhoneNumber;
}
}
What is the mistake I am making here? 我在这里犯了什么错误? I read through a few Context related questions and threads, Not able to figure out what's going wrong in my code as I am noob here.
我通读了一些与Context相关的问题和线索,由于我不是菜鸟,所以无法弄清楚代码中出了什么问题。
Edit : My question is, When I move the telephony manager part to the application class, it doesn't return a phone number. 编辑:我的问题是,当我将电话管理器部分移至应用程序类时,它不会返回电话号码。 Why is that?
这是为什么?
@55597 @ 55597
Please use the following piece of code.You got your problem 请使用以下代码。您遇到了问题
TelephonyManager tMgr =(TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
You are not passing the correct context of Activity for TelephonyManager, So that its return null. 您没有为TelephonyManager传递正确的Activity上下文,因此其返回null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.