5

I want to get the location and time and date of the outgoing call in android.Here i'm using one broadcast receiver for detecting new outgoing calls, within the broadcast receiver i'm starting one service which can implements the location receiver, for get the current location for each and every new outgoing call, it will trigger.

My question is:

  • I want current location,time, date of outgoing call.

Broadcast Receiver

public class OutGoingCallReceiver extends BroadcastReceiver {
public static String phoneNumber;

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

    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    if (state == null) {

        phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Intent serviceIntent = new Intent(context, NarenService.class);
        context.startService(serviceIntent);
        Toast.makeText(context, "Calling..to  " + phoneNumber, Toast.LENGTH_SHORT).show();
    }
  }
}

Service

public class NarenService extends Service implements LocationListener {
SQLiteDatabase db;
double lat, lon;
LocationManager locationManager;
String provider;

DataProvider dataProvider;
Cursor cursor;
private static final String TAG = "NarenService";

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    getLocation();
   // OutGoingCallReceiver.phoneNumber = str;
    //  OutGoingCallReceiver outGoingCallReceiver = new OutGoingCallReceiver()
    dataProvider = new DataProvider(this);
    if (lon != 0.0 && lat != 0.0) {
        dataProvider.open();

        dataProvider.insertEntry(lon, lat, OutGoingCallReceiver.phoneNumber);
        Log.v(TAG, "Inserted data");
        // dataProvider.close();
        Toast.makeText(getBaseContext(), "Lat : " + lat + "\n lon : " + lon + "\n num :" + OutGoingCallReceiver.phoneNumber, Toast.LENGTH_SHORT).show();
    }

    stopSelf();
    return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}


private void getLocation() {
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);

    if (provider != null && !provider.equals("")) {

        // Get the location from the given provider
        Location location = locationManager.getLastKnownLocation(provider);
        locationManager.requestLocationUpdates(provider, 60000, 10, this);

        if (location != null) {
            Log.v(TAG, "Entered into loc");
            onLocationChanged(location);
        } else
            Toast.makeText(getBaseContext(), "Location can't be retrieved", Toast
                    .LENGTH_SHORT).show();

    } else {
        Toast.makeText(getBaseContext(), "No Provider Found", Toast.LENGTH_SHORT).show();
    }

}

@Override
public void onLocationChanged(Location location) {
    lat = location.getLatitude();
    lon = location.getLongitude();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public void onDestroy() {
    super.onDestroy();
    //  dataProvider.close();
    Toast.makeText(this, "service stopped", Toast.LENGTH_SHORT).show();
}
}
Narendra Baratam
  • 828
  • 1
  • 12
  • 26

2 Answers2

4

Use this code for call endings

case TelephonyManager.CALL_STATE_IDLE:
                //Went to idle-  this is the end of a call.  What type depends on previous state(s)
                if(lastState == TelephonyManager.CALL_STATE_RINGING){
                    //Ring but no pickup-  a miss
                    onMissedCall(savedNumber, callStartTime);
                }
                else if(isIncoming){
                    onIncomingCallEnded(savedNumber, callStartTime, new Date());                        
                }
                else{
                    onOutgoingCallEnded(savedNumber, callStartTime, new Date());                                                
                }
                break;
Kiran Kumar
  • 677
  • 3
  • 17
  • 2
    follow this ink for more clarity http://stackoverflow.com/questions/21990947/detect-the-end-of-an-answered-incoming-call-by-user-in-android-not-declined – Kiran Kumar Dec 14 '15 at 07:29
4

Google introduced Fused Location Provider api's via its Google Play Service Framework which is super easy to use than GPS / Network Location providers.

Compatible upto API Level 8, now when i get this weird issue of Network provider, same time Fused Location gives me accurate location(without device restart).

I have commited one working FusedLocation Test Project here, you can clone and test yourself..

LocationListener of NETWORK_PROVIDER is enabled but , onLocationChanged is never called

Community
  • 1
  • 1
Anil Kumar
  • 41
  • 2