Use TelephonyManager in fragment? - android

I want to start and stop service when device in action call, so i use TelephonyManager and my class extends flagment. i Tried this code
TelephonyManager tm = (TelephonyManager)getActivity().getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
private PhoneStateListener mPhoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
getActivity().stopService(new Intent(getActivity(),LockScreenService.class));
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
getActivity().stopService(new Intent(getActivity(),LockScreenService.class));
break;
case TelephonyManager.CALL_STATE_IDLE:
getActivity().startService(new Intent(getActivity(),LockScreenService.class));
break;
}
}
}
;
but it does not work. Please give me some advises. Thanks so much

Try this code:
telephonyManager = ( TelephonyManager )getActivity.getSystemService( Context.TELEPHONY_SERVICE );
imeistring = telephonyManager.getDeviceId();

use Telephony manager inside your oncreate view

Related

How to check phone service state in android

I am developing an app in which i am trying to check the service state of phone
using Service state: IN_SERVICE
if the phone is IN_SERVICE generate a Toast phone is in service if its not then generate a Toast phone is not in service
im trying this code
public class MyphoneStateListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
sendemail();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
not able to get the Service state: IN_SERVICE Please help me
Thanks in advance
TelephonyManager telMng = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telMng.listen(new PhoneStateListener() {
#Override
public void onServiceStateChanged (ServiceState serviceState) {
super.onServiceStateChanged(serviceState);
String phonestate;
switch(serviceState.getState()) {
case ServiceState.STATE_EMERGENCY_ONLY:
phonestate ="STATE_EMERGENCY_ONLY";
break;
case ServiceState.STATE_IN_SERVICE:
phonestate ="STATE_IN_SERVICE";
break;
case ServiceState.STATE_OUT_OF_SERVICE:
phonestate ="STATE_OUT_OF_SERVICE";
break;
case ServiceState.STATE_POWER_OFF:
phonestate ="STATE_POWER_OFF";
break;
default:
phonestate = "Unknown";
break;
}
}
}, PhoneStateListener.LISTEN_SERVICE_STATE);

Handling TelephonyManager (Handling Incoming calls, Dialed, Missed calls) : Is this possible in Android versions from 2.3

I heard that we can not use TelephonyManager for detecting Incoming/Missed/Dialed calls from Android version 2.3 and above. Is this true? Incase yes, Do we have any workaround for handling the calls? Please suggest. Im in urgent need of it.
Check this post. It will help.
You can query it.
Please find the code to use TelephonyManager. I tried in 2.3.3 and is working for me :
public class MainActivity extends Activity {
private TelephonyManager telephonyManager;
private PhoneStateListener listener;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the telephony manager
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// Create a new PhoneStateListener
listener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
String stateString = "N/A";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing";
break;
}
Log.i("DEMO",
String.format("\nonCallStateChanged: %s", stateString));
}
};
telephonyManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
In Manifest, you need to put the permission :
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

Trouble with reading phone state

I want to perform some operation (Pause game) in my application when a call came. But reading the phone state is not working. I have given permission(READ_PHONE_STATE) in the manifest. Nothing is happen when a call came.
Thanks.
TelephonyManager telephonyManager;
PhoneStateListener listener;
telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
*
*
*
listener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(SudokuGameActivity.this, "IDLE", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(SudokuGameActivity.this, "OFF Hook", Toast.LENGTH_SHORT).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(SudokuGameActivity.this, "Ringing", Toast.LENGTH_SHORT).show();
mpauseButton.performClick();
break;
}
}
};
Have you written the following line :
telephonyManager.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
when your listener has be created, you need invoke `public void listen (PhoneStateListener listener, int events)' to listen.
also, you can try this:
create a broadcatst receiver handle the action android.intent.action.PHONE_STATE,
code example:
public class PhoneStateReceiver extends BroadcastReceiver {
private TelephonyManager manager;
#Override
public void onReceive(Context context, Intent intent) {
if (manager == null) {
manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
}
String action = intent.getAction();
System.out.println(action);
System.out.println("current phone state:" + manager.getCallState());
}
}

Broadcast receiver and phonestatelistener

I am trying to use BroadcastReceiver to receive incoming calls. I want to register call when the call is ended. Below code gives each state (idle,ringing n offhook) more than once with the same number (its not an issue of past call). Is it because phonestatelistner is called within the service.
public class IncomingCallReceiver extends BroadcastReceiver {
int stateString =0;
int prevStat = 0;
#Override
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
if(null == bundle)
return;
String state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
Toast.makeText(context, info, Toast.LENGTH_LONG).show();
}
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
//CustomPhoneStateListener customPhoneListener = new CustomPhoneStateListener();
PhoneStateListener listener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
String p = incomingNumber;
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
prevStat = stateString;
stateString = 1;
Log.i("ON CALL STATE CHANGED","IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("ON CALL STATE CHANGED","OFFFHOOK");
prevStat = stateString;
stateString = 2;
break;
case TelephonyManager.CALL_STATE_RINGING:
prevStat = stateString;
stateString = 3;
Log.i("ON CALL STATE CHANGED","RINGING");
break;
}
if(prevStat == 2 && stateString == 1)
{
Log.i("REGISTER A CALLL","==" +p);
}
}
};
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
I also tried the same thing with phonestatelistener in a separate class, but it was not being called, i.e my code didn't reach there. In my manifest file I have registered receiver, but there is no declaration of phonestatelistener. Is that to be registered?
I am sorry if the solution is quite obvious but am new to android things.
Any help is appreciated :)
Yes, choose listener or received , not both of them

Streaming issue in android

I have streamed audio from url,but when the call or sms comes i need to pause the song and when the call ends again i am gonna resume the song...
How to do this?...
This method works in my app, it's pretty simple. In your onCreate() put this code to listen for phone events and then act appropriately:
TelephonyManager telephonyManager;
PhoneStateListener ringListener;
// Get the telephony manager
telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
// Create a new PhoneStateListener
ringListener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
String stateString = "none";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing";
Log.i(TAG, "TELEPHONE RINGING!!");
//Do stuff here when phone ringing detected
break;
}
}
};
// Register the listener with the telephony manager
telephonyManager.listen(ringListener, PhoneStateListener.LISTEN_CALL_STATE);
//End create a new PhoneStateListener

Categories

Resources