-1

I'm new in android programming.So please help me.. I'm developing an app in which it continuously monitors the current location.I want he app to run in background even if the user goes for an other app.He will be able to stop the location updates whenhe returns to my app.Everything is working fine until i do an outgoing call.I'm not able to remove location updates after the outgoing call..

Below is my code..

tb_trackstartstop.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub

            if(isChecked)
            {               
                trackflag=true;                 
                tracklistener(HomeActivity.this);                   
            }
            else
            {
                if(trackflag)
                    trackmlocManager.removeUpdates(trackmlocListener);
                trackflag=false;
                Toast.makeText(HomeActivity.this, "Stopped", Toast.LENGTH_LONG).show();                 
            }               
        }

and my code for onpause and onresume is as follows

@Override
protected void onPause()
{
    super.onPause();
    bundle.putBoolean("trackflagState", trackflag);
}


@Override
protected void onResume()
{
    super.onResume();
    trackflag=bundle.getBoolean("trackflagState",false);

}
Lal
  • 14,726
  • 4
  • 45
  • 70

1 Answers1

1

Use BroadcastReceiver

Register receiver in Manifest:

<receiver android:name=".MyCallReciever">
<intent-filter>
    <action android:name="android.intent.action.PHONE_STATE" />
    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>

Permission in manifest:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Receiver class:

public class MyCallReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
                        TelephonyManager.EXTRA_STATE_OFFHOOK)) {

            //Unregister the location listener here.
        }

    }
}

Hope this helps.

MysticMagicϡ
  • 28,593
  • 16
  • 73
  • 124
  • Thanks for ur answer..But Broadcast Receiver works for incoming calls..I think so..I want the app to run as normal after performing an outgoing call and should removelocation update only after i change the state of the togglebutton. – Lal Jan 08 '14 at 11:28
  • Add `` in intent filter. I think it should work. Please let me know after you try. – MysticMagicϡ Jan 08 '14 at 11:31
  • The above code is giving a null pointer exception on `MyCallReceiver` – Lal Jan 08 '14 at 12:04
  • Where do you get NPE? @Lal – MysticMagicϡ Jan 08 '14 at 12:07
  • NullPointerException in `MyCallReceiver` @Mystic – Lal Jan 08 '14 at 12:10
  • On which line and what is null? Please debug that. And refer [this](http://i-gorod.org/itblog/2012/02/29/detecting-incoming-and-outgoing-calls-in-android/) @Lal – MysticMagicϡ Jan 08 '14 at 12:12
  • First of all Can i ask u a doubt..How can i register the receiver such that the `onReceive` is called only when the toggle button is unchecked @Mystic – Lal Jan 08 '14 at 12:18
  • @Lal Check [this](http://stackoverflow.com/a/4805733/1777090). You can call registerReceiver and unregisterReceiver in your OnCheckedChangeListener listener according to checked status. – MysticMagicϡ Jan 08 '14 at 12:23
  • Thanks for ur help @Mystic..But now i found out that in my app `trackmlocManager.removeUpdates(trackmlocListener)` is not working after i minimise the app and return back.Pls help.@Mystic – Lal Jan 09 '14 at 06:57
  • Do i need to save `trackmlocManager` to a bundle like `bundle.putBoolean("trackflagState", trackflag);` in onPause and retrieve that in onResume ?How to do that? @Mystic – Lal Jan 09 '14 at 07:32
  • It worked..Thanks a lot..:)But now i got another problem.The location listener is not listening to updates when minimised.Works perfectly when resumed @Mystic – Lal Jan 09 '14 at 11:45
  • @Lal So you want to listen to updates when app is in background? – MysticMagicϡ Jan 09 '14 at 12:00
  • yes..my gpstracker class extends `Service` and still it isn't working..@Mystic – Lal Jan 10 '14 at 05:52
  • Sorry @Lall Don't know :( – MysticMagicϡ Jan 10 '14 at 07:13
  • its ok..Thanks for ur support..:) @Mystic – Lal Jan 10 '14 at 07:40