In the app that I'm making it requires a change in activity on the end of a phone call. The code I'm using isn't working like the way I want it:
protected class EndCallListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber){
switch(state){
case TelephonyManager.CALL_STATE_OFFHOOK:
Intent intent = new Intent(MainActivity.this, Password.class);
startActivity(intent);
break;
default:
}
super.onCallStateChanged(state, incomingNumber);
}
}
Any ideas?
Related
I have used TelephonyManager.CALL_STATE_OFFHOOK but unfortunately it gets called every time I touch the call button, i.e before the call is actually made.
my code:
public void onReceive(Context context, Intent intent) {
this.context = context;
listener = new CallStateListener();
if(intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")){
Log.e("Aditya", "Broadcast listner");
//Currently no use
}
TelephonyManager tm = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
tm.listen(listener,PhoneStateListener.LISTEN_CALL_STATE);
}
public class CallStateListener extends PhoneStateListener {
int lastState = TelephonyManager.CALL_STATE_IDLE;
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// called when someone is ringing to this phone
Toast.makeText(context,"Pioneer Contacts+ Updated",Toast.LENGTH_LONG).show();
Log.e("Aditya", "ringing");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(context,"Pioneer Contacts+ Updated",Toast.LENGTH_LONG).show();
Log.e("Aditya", "offhook");
break;
}
}
}
The Log Log.e("Aditya", "offhook"); gets printed before the call gets connected. I want to appear it after the call is disconnected.
I want to design an App in which I want to end Incoming/outgoing calls after some specific time. I am using PhoneStateListener to listen state change of call
public class MyPhoneStateListener extends PhoneStateListener {
public void onCallStateChanged(int state,String incomingNumber){
switch(state){
case TelephonyManager.CALL_STATE_IDLE:
tIncomingCounter=false;
Log.i(TAG1, "IDLE"+IncomingCounter);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
IncomingCounter=true;
Log.i(TAG1, "OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.i(TAG1, "RINGING");
break;
}
}
}
I am newbie in android development. So I need help .
i would like to display an activity when phone state is ringing, and kill this activity when state is idle or offhook.
So I built a class extends Broadcastreceiver. I call myphonestatelistener class extends PhoneStateListener . Also i create an activity named ringingactivity.
Here is my code (MyPhoneStateListener):
public void onCallStateChanged(int state,String incomingNumber){
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
Intent killIntentidle = new Intent();
killIntentidle.setAction("RingingOver") ;
ctx.sendBroadcast(killIntentidle);
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "OFFHOOK");
Intent killIntent = new Intent();
killIntent.setAction("RingingOver") ;
ctx.sendBroadcast(killIntent);
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
Intent dialogIntent = new Intent(ctx , RingingActivity.class) ;
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(dialogIntent);
break;
}
}
In my tests, i call emulator from telnet and it succesfully display activity when it is ringing. When i accept or decline call, activity is finished . But when i call emulator again, activity could not be recreated again. I cant see it on the screen. What am i doing wrong?
Here is my activity code :
public class RingingActivity extends Activity {
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.ringing_activity);
IntentFilter filter = new IntentFilter();
filter.addAction("RingingOver");
registerReceiver(receiver, filter);
//registerReceiver(receiver,filter) ;
}
public void finish() {
super.finish();
};
}
Thanks for your help!
I suspect the issue is in your MyPhoneStateListener code. The other code look O.K to me.
Some how your PhoneStateListener is not alive next time. Are you able to see "RINGING" logs for second time?
I am not sure as why you need to call Garbage Collector. Please try to fix the root cause as why, activity is not created; sometimes. First analyze the issue by putting logs in your MyPhoneStateListener and RingingActivity. In MyPhoneStateListener, do print the state of RingingActivity activity, before taking any action (Start/kill activity).
Thank you very much for your reply Munish Katoch. I have a solution but not sure if i find the right solution . when I use Garbage Collecter , activity could be seen in screen in each calling.
Here is my code :
public class MyPhoneStateListener extends PhoneStateListener {
Context ctx ;
private boolean iscalling ;
MyPhoneStateListener(Context ctx)
{
this.ctx = ctx ;
}
public void onCallStateChanged(int state,String incomingNumber){
switch(state)
{
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
if(iscalling == true)
{
Intent killIntentidle = new Intent();
killIntentidle.setAction("RingingOver") ;
ctx.sendBroadcast(killIntentidle);
System.gc() ;
iscalling = false ;
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "OFFHOOK");
if(iscalling == true)
{
Intent killIntent = new Intent();
killIntent.setAction("RingingOver") ;
ctx.sendBroadcast(killIntent);
System.gc() ;
iscalling = false ;
}
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
iscalling = true ;
Intent dialogIntent = new Intent(ctx , RingingActivity.class) ;
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ctx.startActivity(dialogIntent);
System.gc() ;
break;
}
}
}
I need help with app(or service?) basically I was just fooling around, and decided to see if I can make my app send an email if it detects my phone is ringing. (I know it seems stupid but its more of a proof of concept for me. I made custom class in my app (no default main activity) I don't think its getting executed.. I set a toast message to pop up when the app is running, but I never saw one show up. So, I figured I need to do something to make my class execute as if its the default one, and if so, how would I go about doing that? In the android manifest, the only things I added to that were the permissions of read_phone_state, and internet(in case it was needed to send an email)
My question is what would I need to change (no need to debug it.) to make it work? Do I need to add extra info in my manifest file? Do I need to change settings where it will execute "callservice" class when the app first starts? Please, and thanks.
public class MainActivity extends Activity {
PhoneStateListener listener;
TelephonyManager tm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(getApplicationContext(), "Service has started", Toast.LENGTH_LONG).show();
listener = new MyphoneStateListener();
tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
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);
}
}
private void sendemail()
{
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"random#gmail.com"});
i.putExtra(Intent.EXTRA_SUBJECT , "phone is ringing");
i.putExtra(Intent.EXTRA_TEXT , "email successfully sent");
startActivity(Intent.createChooser(i, "send mail..."));
Toast.makeText(getApplicationContext(), "Email has been sent", Toast.LENGTH_LONG).show();
try
{
startActivity(Intent.createChooser(i, "Send Email..."));
}
catch(android.content.ActivityNotFoundException ex)
{
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
as in doc onCallStateChanged take two parameters first int state and second is String incomingNumber but in your current code you are just passing single parameter to it means your are not overriding onCallStateChanged method of PhoneStateListener change your code as to work :
class MyPhoneStateListener extends PhoneStateListener{
#Override
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);
}
}
I need my app to find out when a phone call is taking place.
It should work on when another phone is being called and also when a call is answered. I just need my app to get notified of exactly when the connection starts and when it stops (not the dialing, ringing, etc.).
Is this somehow possible?
You can use broadcast class to achieve this as below:
public class PhoneStateBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
TelephonyManager telephonyManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context),
PhoneStateListener.LISTEN_CALL_STATE);
}
}
class CustomPhoneStateListener extends PhoneStateListener {
// private static final String TAG = "PhoneStateChanged";
Context context; // Context to make Toast if required
public CustomPhoneStateListener(Context context) {
super();
this.context = context;
}
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
//WHEN PHONE WILL BE IDLE
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// when Off hook i.e in call
break;
case TelephonyManager.CALL_STATE_RINGING:
// when Ringing
break;
default:
break;
}
}
}
Hope this will help you.