Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to send street adres via sms.But when i run the app and make the call it show me the null . actually i created broadcast reciver class and one service class. i used the broadcast reciver class as a inner class of the service class as

C#
public class MyService extends Service implements LocationListener {
String result = null;
StringBuilder sb;
List<Address> addressList;
// flag for GPS status
boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;


public Location getLocation() {
    try {
        locationManager = (LocationManager) this
                .getSystemService(LOCATION_SERVICE);

        // getting GPS status
        isGPSEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        // getting network status
        isNetworkEnabled = locationManager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            this.canGetLocation = true;
            // First get location from Network Provider
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

/**
 * Stop using GPS listener
 * Calling this function will stop using GPS in your app
 */
public void stopUsingGPS() {
    if (locationManager != null) {
        locationManager.removeUpdates(MyService.this);
    }
}

/**
 * Function to get latitude
 */
public double getLatitude() {
    if (location != null) {
        latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
}


  private void mygeo()
{
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());



    try {
        addressList = geocoder.getFromLocation(
                latitude, longitude, 3);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    if (addressList != null && addressList.size() > 0) {
        Address address = addressList.get(0);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
            sb.append(address.getAddressLine(i)).append("\n");
        }
        sb.append(address.getLocality()).append("\n");
       // sb.append(address.getPostalCode()).append("\n");
        sb.append(address.getCountryName());
        result = sb.toString();
    }
}


/**
 * Function to get longitude
 * */
public double getLongitude(){
    if(location != null){
        longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
}

/**
 * Function to check GPS/wifi enabled
 * @return boolean
 * */
public boolean canGetLocation() {
    return this.canGetLocation;
}

@Override
public void onLocationChanged(Location location) {

}

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

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    getLocation();
    mygeo();
    Toast.makeText(getBaseContext(),""+latitude+"\n"+longitude+"\n"+result,Toast.LENGTH_LONG).show();


    return START_STICKY;
}


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


       public class Reciver extends BroadcastReceiver {


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


        try {
            if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                String stat = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);


                Toast.makeText(context, "ITS ringing" + stat, Toast.LENGTH_SHORT).show();
                SmsManager smsManager = SmsManager.getDefault();

                smsManager.sendTextMessage(stat, null, "hi" + "\n" + latitude+"\n"+longitude+"\n"+"Street Adress" +result+ "\n" + "\n", null, null);

                Thread.sleep(10000);
                fun_END_Call();


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

            // Toast.makeText(context, "Call Recieved", Toast.LENGTH_LONG).show();

            // }
            // if(intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_IDLE)) {

            //  Toast.makeText(context, "Phone Is Idle", Toast.LENGTH_LONG).show();
            // }


        } catch (Exception e) {

        }

    }


    public void fun_END_Call() {
        try {


            //String serviceManagerName = "android.os.IServiceManager";
            String serviceManagerName = "android.os.ServiceManager";
            String serviceManagerNativeName = "android.os.ServiceManagerNative";
            String telephonyName = "com.android.internal.telephony.ITelephony";

            Class telephonyClass;
            Class telephonyStubClass;
            Class serviceManagerClass;
            Class serviceManagerNativeClass;

            Method telephonyEndCall;

            Object telephonyObject;
            Object serviceManagerObject;

            telephonyClass = Class.forName(telephonyName);
            telephonyStubClass = telephonyClass.getClasses()[0];
            serviceManagerClass = Class.forName(serviceManagerName);
            serviceManagerNativeClass = Class.forName(serviceManagerNativeName);

            Method getService =
                    serviceManagerClass.getMethod("getService", String.class);

            Method tempInterfaceMethod = serviceManagerNativeClass.getMethod(
                    "asInterface", IBinder.class);

            Binder tmpBinder = new Binder();
            tmpBinder.attachInterface(null, "fake");

            serviceManagerObject = tempInterfaceMethod.invoke(null, tmpBinder);
            IBinder retbinder = (IBinder) getService.invoke(serviceManagerObject, "phone");
            Method serviceMethod = telephonyStubClass.getMethod("asInterface", IBinder.class);

            telephonyObject = serviceMethod.invoke(null, retbinder);
            telephonyEndCall = telephonyClass.getMethod("endCall");
            telephonyEndCall.invoke(telephonyObject);
            Log.v("VoiceCall", "Call End Complete.");


        } catch (Exception e) {
            e.printStackTrace();
            Log.e("VoiceCall", "FATAL ERROR: could not connect to telephony subsystem");
            Log.e("VoiceCall", "Exception object: " + e);
        }


    }


}



i want to send this longitude and latitude which declare globaly in the service class to send via sms in the broadcast reciever class. when i get the call it ring for 10 second and then automatically disconnect and send this longitude and latitude and street adres to the get number via sms. but when the app run and make the call ,after 10 second the call become disconnect and the message contain empty i mean null null....

What I have tried:

now how can i send this longitude and latitude via sms getting from the service
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900