I did some googling but couldnt find a clear answer for my question.
I have an activity that need's to be launched when the phone starts to ringing (receives a phone call)- instead of androids default screen .
I know I must set a receiver in Manifest.xml:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name=".ServiceReceiver">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
and create a receiver class :
public class MyPhoneStateListener extends PhoneStateListener {
public static Boolean phoneRinging = false;
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.e("DEBUG", "IDLE");
phoneRinging = false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.e("DEBUG", "OFFHOOK");
phoneRinging = false;
break;
case TelephonyManager.CALL_STATE_RINGING:
//Intent intent=new Intent(getClass().)
Log.e("DEBUG", "RINGING");
phoneRinging = true;
break;
}
}
}
class ServiceReceiver extends BroadcastReceiver {
TelephonyManager telephony;
Intent in;
public void onReceive(Context context, Intent intent) {
MyPhoneStateListener phoneListener = new MyPhoneStateListener();
telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
now I can grab "RINGING" in my logcat correctly but how can I start my custom activity or what is the preferred way to do that?
i found an answer and i'm going to share that with you.
first there is nothing to do with onCallStateChanged
portion .
so i must call target activity in onReceive
inside BroadcastReceiver class :
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
Intent intent1=new Intent(context, Ringing.class);
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
}