In my application I'am using BroadcastReceiver to track telephone state.
There are 3 states available in TelephonyManager.
In my scenario I want to show simple ui which shows some details about the caller while the phone is ringing and i want to close that ui when the call attended by user or call ended.
I'am calling my service when the phone state is Ringing. If my app is in use then I can see my ui part while the phone is ringing. If my app is not in use then I cant see the ui part.
I have checked in my background. The service is not running. I think my service is running only when my app is in use and it is not running when my app is not in use.
To overcome this what I should do?
This is my code.
@Override
public void onReceive(Context context, Intent intent) {
LoginActivity.login.finish();
MainActivity.main.finish();
telephonyRegister(context,intent);
}
public void telephonyRegister(final Context context, Intent intent)
{
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imeiSIM1= telephony.getDeviceId();
System.out.println("IMEI NUMBER"+imeiSIM1);
ctx=context;
if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_IDLE)) {
try
{
ProjectDailogActivity.projdialog.finish();
}catch(Exception e)
{
e.printStackTrace();
}
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_OFFHOOK))
{
try{
ProjectDailogActivity.projdialog.finish();
}catch(Exception e)
{
e.printStackTrace();
}
}
else if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
TelephonyManager.EXTRA_STATE_RINGING)){
phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
isringing=true;
Intent intent11 = new Intent(ctx,ProjectDailogActivity.class);
intent11.putExtra("incoming_number",phoneNumber);
intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(intent11);
}
}
Manifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name="com.domyhome.broadcastreceiver.IncomingCallInterceptor" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
I got the window like truecaller.
Just the values which i mentioned are default one.
But the same window is not coming when my app is not in use.
Please give me an idea.
that is why android developers invented the BroadcastReciever - it allows you to hook your code as a service in response for system events (such as boot, airplane mode, etc)
Vogella covers it very well: http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html
and for your situation (send commands as a response for the phone state) you can also use this article which provide a fully working example: http://androidexample.com/Incomming_Phone_Call_Broadcast_Receiver__-_Android_Example/index.php?view=article_discription&aid=61&aaid=86
article hightlights:
<!-- Manifest.xml -->
<receiver android:name=".IncomingCall">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
and in java - add these classes
//your Listener class:
private class MyPhoneStateListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
// state = 1 means when phone is ringing
if (state == 1)
{
// do something
}
}
}
//Reciever class:
public class IncomingCall extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
// TELEPHONY MANAGER class object to register one listner
TelephonyManager tmgr = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
//Create Listner
MyPhoneStateListener PhoneListener = new MyPhoneStateListener();
// Register listener for LISTEN_CALL_STATE
tmgr.listen(PhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}