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.
Related
I am using this method to end a call in android
public boolean killCall(Context context) {
try {
// Get the boring old TelephonyManager
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
// Get the getITelephony() method
Class classTelephony = Class.forName(telephonyManager.getClass().getName());
Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");
// Ignore that the method is supposed to be private
methodGetITelephony.setAccessible(true);
// Invoke getITelephony() to get the ITelephony interface
Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);
// Get the endCall method from ITelephony
Class telephonyInterfaceClass =
Class.forName(telephonyInterface.getClass().getName());
Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
// Invoke endCall()
methodEndCall.invoke(telephonyInterface);
} catch (Exception ex) { // Many things can go wrong with reflection calls
Log.d(TAG, "PhoneStateReceiver **" + ex.getCause().getStackTrace());
return false;
}
return true;
}
This does work before Oreo but does not working on android oreo any idea what is missing.
Best Regards: Ali
I do your code and follow the explain:
End call in android programmatically
add the following permission
<uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
then it's working very well, my code is running on android oreo version.
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 use the following code to handle incoming calls from within a service. It works perfectly when i invode the endCall method (My phone's Android Version is ICS). But when I invoke the answerRingingCall method i get an exception for not having permission to modify phone state. I know that Google revoked this permission at one point but since i can invoke the end call method what is the explanation for not being able to invoke the answer call method as well? I mean..both methods modify the phone's state, so what's up? Is there a way to fix this?
try {
TelephonyManager tm = (TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
Class c = Class.forName(tm.getClass().getName());
Method m = c.getDeclaredMethod("getITelephony");
m.setAccessible(true);
Object telephonyService = m.invoke(tm); // Get the internal ITelephony object
c = Class.forName(telephonyService.getClass().getName()); // Get its class
m = c.getDeclaredMethod("endCall"); // Get the "endCall()" method
m.setAccessible(true); // Make it accessible
m.invoke(telephonyService); // invoke endCall()
}
catch (Exception e) {
Log.w("exception",e);
}
If I had to guess, I'd say in your OEM version of com.android.phone.PhoneInterfaceManager inside the Phone.apk the endCall function doesn't call enforceCallPermission(); before sending the request to end the call.
Where as in the answerRingingCall function calls enforceCallPermission();
You could try a hack where you get access the the private method, on the ITelephony object, sendRequestAsync set accessible to true, and then call it with 4 as the input param, aka CMD_ANSWER_RINGING_CALL.
This would avoid the permissions check.
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
In some cases I don't want listen to state of my phone.
How to destroy object of PhoneStateListener class?
I create object this way
try {
phoneCallListener = new WnetPlayerPhoneCallListener();
TelephonyManager mTM = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
mTM.listen(phoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
} catch(Exception e) {
Log.e("PhoneCallListener", "Exception: "+e.toString());
}
In the documentation it states to pass the listener object and flag LISTEN_NONE to unregister a listener.
Per this answer, you should keep a reference to TelephonyManager and WnetPlayerPhoneCallListener, and set it to disabled, like so:
mTm.listen(phoneCallListener, PhoneStateListener.LISTEN_NONE);
Why they don't just have standard addListener() and removeListener() methods, I don't know, but this seems to be the accepted method for solving your problem.