onReceive()在未接来电检测器程序中调用两次

时间:2017-12-11 09:03:01

标签: android

我希望在接到未接来电时以短信的形式发送当前位置

public class IncommingCallReceiver extends BroadcastReceiver {


    static boolean ring = false;
    static boolean callReceived = false;
    static String callerPhoneNumber;
    private LocationManager locationManager;
    public String str;
    private LocationListener listener;
    @Override
    public void onReceive(Context mContext, Intent intent) {

        Log.e("Called", "OnReciver");




        // Get the current Phone State
        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

        if (state == null)
            return;


        // If phone state "Rininging"
        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            // Get the Caller's Phone Number
            Bundle bundle = intent.getExtras();
            callerPhoneNumber = bundle.getString("incoming_number");

            ring = true;



        }


        // If incoming call is received
        if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            callReceived = true;

        }


        // If phone is Idle
        if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            // If phone was ringing(ring=true) and not received(callReceived=false) , then it is a missed call
            if (ring == true && callReceived == false) {
                Toast.makeText(mContext, "It was A MISSED CALL from : " + callerPhoneNumber, Toast.LENGTH_LONG).show();

                str=getLocation(mContext);
                Toast.makeText(mContext, "your current Location : "+str, Toast.LENGTH_LONG).show();

            }
        }

    }



    public String getLocation(final Context mContext)
    {



        listener = new LocationListener() {


            @Override
            public void onLocationChanged(Location location) {

                Intent i = new Intent("location_update");
                double latitude=location.getLatitude();
                double longitude=location.getLongitude();

                Geocoder geocoder = new Geocoder(mContext);

                try {
                    List<Address> addressList = geocoder.getFromLocation(latitude,longitude,1);
                   str = addressList.get(0).getLocality()+",";

                   Toast.makeText(mContext,"Message is going to send"+str,Toast.LENGTH_SHORT).show();
                   // sendSMS(callerPhoneNumber,"The current location  is "+str, mContext);
                } catch (IOException e) {
                    e.printStackTrace();
                  Toast.makeText(mContext, "inside Catch", Toast.LENGTH_LONG).show();
                }

                String msg=str;
                i.putExtra("coordinates",msg);
                i.putExtra("coordinates",location.getLongitude()+""+location.getLatitude());
                //sendBroadcast(i);

            }

            public void sendSMS(String phoneNo, String msg, final Context mContext) {
                try {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(phoneNo, null, msg, null, null);
                    Toast.makeText(mContext, "Message Sent",
                            Toast.LENGTH_LONG).show();
                    locationManager.removeUpdates(listener);
                } catch (Exception ex) {
                    Toast.makeText(mContext,ex.getMessage().toString(),
                            Toast.LENGTH_LONG).show();
                    ex.printStackTrace();
                }
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {
                Intent i = new Intent(Settings.ACTION_LOCALE_SETTINGS);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(i);
            }
        };

        locationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return "" ;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, listener);


        return str;
    }
}

您期望结果如何?

您获得的实际结果是什么? (请包含任何错误。)

0 个答案:

没有答案