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
Related
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.
I need to get LAC and Cid codes in my Android application. However, I need to do it inside a not-Activity class. The code I found is this:
TelephonyManager telephonyManager =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
int cid = cellLocation.getCid();
int lac = cellLocation.getLac();
However, the method setSystemService is present only in Activity classes, and I have not found something to send "some sort of activity" to the class.
Is there any way of do so, without activity ?
You can pass context as an argument to constructor of the class. Inside the constructor you can initialize the TelephonyManager.
Example :
In class,
public class MyClass {
private TelephonyManager mTelephonyManager;
public MyClass(Context context) {
mTelephonyManager =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
}
}
In Activity,
public class MyActivity extends Activity {
....
//Initializing class
MyClass myClass = new MyClass(this);
..
You just need to pass a Context object to call getSystemService() from it. You can also use an Activity object as Activity extends Context.
Then just call context.getSystemService()
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.
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().
There is a "SIM card lock" option in android "setting/Location & security settings" page.
It's necessary to input a PIN code after booting if the option is set.
Is there any programmatic method to detect if PIN is required ? (not current sim state but the setting option value ex: true/false)
You can use the following class:
TelephonyManager
http://developer.android.com/reference/android/telephony/TelephonyManager.html
You do not instantiate this class directly; instead, you retrieve a reference to an instance through Context.getSystemService(Context.TELEPHONY_SERVICE)
TelephonyManager manager = (TelephonyManager) Context.getSystemService(Context.TELEPHONY_SERVICE);
int state = manager.getSimState();
if(state == TelephonyManager.SIM_STATE_PIN_REQUIRED || state == TelephonyManager.SIM_STATE_PUK_REQUIRED)
{
//PIN/PUK is required
}
Following the comments, this is the final version:
TelephonyManager tm =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
Class clazz = Class.forName(tm.getClass().getName());
Method m = clazz.getDeclaredMethod("getITelephony");
m.setAccessible(true);
ITelephony it = (ITelephony) m.invoke(tm);
if(it.isSimPinEnabled())
{
//This should work;
}
As getSimLockEnabled always returns false for me, i had to find another way to do it.
See https://stackoverflow.com/a/12748638/314089 for the answer.