URL - 通过短信发送当前位置,在经纬度中保持返回 0,0 - Android studio

问题描述 投票:0回答:1

我正在尝试获取经度和纬度并通过短信将其发送到已保存的联系人。但是经度和纬度在我的 URL 中返回 0,0。 请你帮我解决这个问题。我尝试了一些我在这里找到的解决方案,但它对我不起作用。 这是我的代码:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // check for runtime permissions
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_DENIED) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.SEND_SMS, Manifest.permission.READ_CONTACTS}, 100);
        }
    }

    // this is a special permission required only by devices using
    // Android Q and above. The Access Background Permission is responsible
    // for populating the dialog with "ALLOW ALL THE TIME" option
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        requestPermissions(new String[]{Manifest.permission.ACCESS_BACKGROUND_LOCATION}, 100);
    }



    button1 = findViewById(R.id.Button1);
    listView = (ListView) findViewById(R.id.ListView);
    db = new DbHelper(this);
    list = db.getAllContacts();
    customAdapter = new CustomAdapter(this, list);
    listView.setAdapter(customAdapter);
    send = findViewById(R.id.send);

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // calling of getContacts()
            if (db.count() != 5) {
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, PICK_CONTACT);
            } else {
                Toast.makeText(MainActivity.this, "Can't Add more than 5 Contacts", Toast.LENGTH_SHORT).show();
            }
        }
    });
    send.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Location object = new Location("service Provider");
            double lat = object.getLatitude(); double lng = object.getLongitude();
            onSuccess(object);
        }
    });

}

public void onSuccess(Location currentLocation) {

    String uri = "http://maps.google.com/maps?saddr=" + currentLocation.getLatitude()+","+currentLocation.getLongitude();
    StringBuffer smsBody = new StringBuffer();
    smsBody.append(Uri.parse(uri));
        // get the SMSManager
        SmsManager smsManager = SmsManager.getDefault();

        // get the list of all the contacts in Database
        DbHelper db = new DbHelper(MainActivity.this);
        List<ContactModel> list = db.getAllContacts();

        // send SMS to each contact
        for (ContactModel c : list) {
            String message = "Hey, " + c.getName() + "I am in DANGER, i need help. Please urgently reach me out. Here are my coordinates.\n " + uri;
            smsManager.sendTextMessage(c.getPhoneNo(), null, message, null, null);
        }
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == 100) {
        if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
            Toast.makeText(this, "Permissions Denied!\n Can't use the App!", Toast.LENGTH_SHORT).show();
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    // get the contact from the PhoneBook of device
    switch (requestCode) {
        case (PICK_CONTACT):
            if (resultCode == Activity.RESULT_OK) {

                Uri contactData = data.getData();
                Cursor c = managedQuery(contactData, null, null, null, null);
                if (c.moveToFirst()) {

                    String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                    String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    String phone = null;
                    try {
                        if (hasPhone.equalsIgnoreCase("1")) {
                            Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
                            phones.moveToFirst();
                            phone = phones.getString(phones.getColumnIndex("data1"));
                        }
                        String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        db.addcontact(new ContactModel(0, name, phone));
                        list = db.getAllContacts();
                        customAdapter.refresh(list);
                    } catch (Exception ex) {
                    }
                }
            }
            break;
    }
}

我正在使用安卓版的奥利奥。 对于清单:SEND_SMS、INTERNET、ACCESS_COARSE_LOCATION、ACCESS_FINE_LOCATION、READ_CONTACTS。

java android location sms
1个回答
0
投票

您正在自己创建

Location
的实例:

Location object = new Location("service Provider");

那将有一个位置 0,0 因为你没有告诉它位置在哪里。

如果您希望实际获取设备位置,则不能像现在这样自己创建

Location
的实例。你需要使用
LocationManager
获得
Location
.

© www.soinside.com 2019 - 2024. All rights reserved.