When I try to use telephony manager to retrieve the phone number from an Activity class, I am able to do it successfully. 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.
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
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.
Related
I am trying to implement GCM Push notification and try to get Android emulator id, and add the following code, but it shows me the following error. I cam relatively new on this platform.
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String android_id= Settings.Secure.getString(getContext().getContentResolver(), Settings.Secure.ANDROID_ID);
}
getContext() method available in View class. Use MainActivity.this to access getContentResolver method:
String android_id= Settings.Secure.getString(MainActivity.this.getContentResolver(),
Settings.Secure.ANDROID_ID)
Hey here is the solution for your question:-
You are using activity so You don't need to use getcontext() or this
Just use below line of code to get you the string:-
String string =Settings.Secure.getString( getContentResolver(),Settings.Secure.ANDROID_ID);
You can use this
String android_id= Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
I want to access the TELEPHONY_SERVICE system service in the Application class, but my app crashes when I run it.
public class SimpleDhtApplication extends Application {
TelephonyManager tel = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String portStr = tel.getLine1Number().substring(tel.getLine1Number().length() -4);
final String myPort = String.valueOf((Integer.parseInt(portStr) * 2));
}
I think I am not accessing the context correctly, can somebody help!
Override the onCreate method of the Application class first. Within the onCreate, put the code you have so it would look like:
public class SimpleDhtApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
TelephonyManager tel = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String portStr = tel.getLine1Number().substring(tel.getLine1Number().length() -4);
final String myPort = String.valueOf((Integer.parseInt(portStr) * 2));
}
}
Update: Watch out for tel.getLine1Number(); it may return null as explained here.
This question already has answers here:
The method getSystemService(String) is undefined for the type Listen
(3 answers)
Closed 9 years ago.
In my Android app I wanna generate the device ID. At first I just generate the device ID inside an activity as below. It worked fine.
TelephonyManager tManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String uid = tManager.getDeviceId();
return uid;
Then I wanna create a device object and try to do the above in it as a method,
public String generateDeviceId() {
// DeviceId = deviceId;
TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String uid = tManager.getDeviceId();
return uid;
}
It gives an syntax error and say
The method getSystemService(String) is undefined for the type Device
So how can I fix this. I wanna create a device class and create device object and do my stuff. Is it possible. I imported the below,
import android.content.Context;
import android.telephony.TelephonyManager;
So is it possible. Can someone help me to do my work. Thanks.
getSystemService(String) needs a context. So you need to pass context to the constructor of Non Activity class and use it there.
new Device(ActivityName.this);
Then
Context mContext;
public Device(Context context)
{
mContext = context;
}
Then
TelephonyManager tManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
http://developer.android.com/reference/android/content/Context.html#getSystemService(java.lang.String)
Try changing this method in your Device class
public String generateDeviceId(Context context) {
// DeviceId = deviceId;
TelephonyManager tManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String uid = tManager.getDeviceId();
return uid;
}
and While calling from your Activity , just pass the current activity reference this
deviceObject.generateDeviceId(this);
When you try to access getSystemService outside of the android activity class then you must need the context.
TelephonyManager tm =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
and this are complete method
public String generateDeviceId(Context context) {
// DeviceId = deviceId;
TelephonyManager tManager = (TelephonyManager)context. getSystemService(Context.TELEPHONY_SERVICE);
String uid = tManager.getDeviceId();
return uid;
}
Thanks
I'm trying to creat an app that would save the current signal strength. So far I've seen many examples that are all extending PhoneStateListener, but all of them use a Toast to display this information, like in this example:
http://www.firstdroid.com/2010/05/12/get-provider-gsm-signal-strength/
I was wondering a couple of things:
a) Do I always have to use the PhoneStateListener AND override the onSignalStrengthsChanged(SignalStrength signalStrength) ?
b) How can I access the value signalStrength.getGsmSignalStrength() from outside the PhoneStateListener class?
Thanks in advance
U can try with it. I cant say its the final solution.If u want to access certain data from other activity, then try it with shared preference.Shared preference value is visible from other activities
I might be late to answer your question, but if you are still looking for the answer, here it is:
a) yes you have to use the PhoneStateListener and override the onSignalStrengthsChanged as, in my knowledge that is the only way to get current cells' signal strength for GSM. The listener is only called in big signal strength changes, so you yourself cannot control the listener. the listener will automatically update or make a toast when it is called. So, it is better to declare the listener and ask it to listen at onCreate().
b) For accessing the the RSSI value from outside the Listener is not really difficult, just store the value in a variable and make a method like getRSSI(), which will return you the value when its called. The example is given below:
public class GsmRSSI extends Activity{
MyPhoneStateListener MyListener;
TelephonyManager Tel;
ArrayList<String> signalStrength = new ArrayList<String>();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyListener = new MyPhoneStateListener();
Tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
Tel.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
timer.schedule(new TimerTask() {
public void run() {
String rssi = MyListener.getStrength();
if(!rssi.equals(""))
signalStrength.add(rssi);
}
}, 0, 5000);//it will add the rssi value after every 5000ms
}
private class MyPhoneStateListener extends PhoneStateListener {
String gsmStrength = "";
#Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
gsmStrength = String.valueOf(signalStrength.getGsmSignalStrength()* 2 - 113);
}
public String getStrength() {
return gsmStrength;
}
}
}
This should do the work for you. But at the begining you might not get any rssi value for a little while as the listener is only called when there is a significant change in the rssi.
I my application I have written following code in order to extract phone number. When I run it in emulator, everything is fine but when I run it on a real device, application crashes. what is your suggestion? I have added <uses-permission android:name="android.permission.READ_PHONE_STATE"/> in manifest file.
public class TestActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView phoneNumber = (TextView)findViewById(R.id.tvPhoneNumber);
phoneNumber.setText(getMy10DigitPhoneNumber());
}
private String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
private String getMy10DigitPhoneNumber(){
String s = getMyPhoneNumber();
return s.substring(2);
}
}
getLine1Number() will return the phone number string if available and null if not available. So you should check for Null Pointer.
private String getMy10DigitPhoneNumber() {
String s = getMyPhoneNumber();
if(s == null) return "";
else return s.substring(2);
}
Also check the length of the string returned by getLine1Number(). In my phone, i got a ""string. In this case, substring() will throw IndexOutOfBoundsException. So check for length of s also before calling substring().