通过消息发送位置时发生问题

时间:2019-03-07 06:24:43

标签: java android location sms geocoder

我想通过消息发送位置,但是有时它发送消息,有时它发送空字符串,有时不发送。

  

这是我要向其发送消息的按钮单击   收件人使用当前位置

     b.setOnClickListener(new View.OnClickListener() {
                String loc;
                @Override
                public void onClick(View view) {
                    arrno[0]=num1;
                    arrno[1]=num2;
                    arrno[2]=num3;
                    arrno[3]=num4;

                    requestpermisson();
                    for (int i = 0; i < arrno.length; i++) {
*here the getter data is stored in the loc String* 
                        loc=getMyLocation();
                        sendSMS(arrno[i], "I am in trouble :\n"+"My current Address :"+loc);

                    }
  

这只是为了显示当前位置和手机的消息   数字       测试

problem is that I get the location   and it sends the SMS with location and sometimes I get a null string and sometimes it does not send
     

SMS(可能是由于超出了消息限制)

                    Toast.makeText(getApplicationContext(), "" + num1 + "" + num2 + "" + num3 + "" + num4+" " +loc , Toast.LENGTH_LONG).show();
                }
            });



            locationManager =(LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

            locationListener=new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {

                    if(location!=null) {
                        Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
                        try {
                            List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);


                            if (listAddresses != null && listAddresses.size() > 0) {
                                String address = "";

                                if (listAddresses.get(0).getAddressLine(0) != null) {
                                    address += listAddresses.get(0).getAddressLine(0) + " ";
                                }

                                if (listAddresses.get(0).getAddressLine(1) != null) {
                                    address += listAddresses.get(0).getAddressLine(1) + " ";

                                }

                                if (listAddresses.get(0).getAdminArea() != null) {
                                    address += listAddresses.get(0).getAdminArea() + " ";
                                }

                                if (listAddresses.get(0).getLocality() != null) {
                                    address += listAddresses.get(0).getLocality() + " ";
                                }





                                // this will show you your current address
                                Toast.makeText(MainActivity.this, address, Toast.LENGTH_SHORT).show();

                                setMyLocation(address);


                            }


                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                    else
                    {
                        Toast.makeText(context, "no location found", Toast.LENGTH_SHORT).show();


                    }


                }

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

                }

                @Override
                public void onProviderEnabled(String provider) {

                }

                @Override
                public void onProviderDisabled(String provider) {

                }
            };






            if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)!=PackageManager.PERMISSION_GRANTED)
            {
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},1);


            }
            else
            {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
            }








        }

        private static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 1;
  

这是用于请求许可

        public void requestpermisson() {
            if (ContextCompat.checkSelfPermission(this,
                    Manifest.permission.SEND_SMS)
                    != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                        Manifest.permission.SEND_SMS)) {
                } else {
                    ActivityCompat.requestPermissions(this,
                            new String[]{Manifest.permission.SEND_SMS},
                            MY_PERMISSIONS_REQUEST_SEND_SMS);
                }
            }
        }






 @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull             String[] permissions, @NonNull int[] grantResults) {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED) {

                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
                }
            }

        }
  

这是短信发送功能

        public void sendSMS(String phoneno, String msg){
            Toast.makeText(getApplicationContext(),"In show m",Toast.LENGTH_SHORT).show();

            try{
                SmsManager smsManager=SmsManager.getDefault();
                smsManager.sendTextMessage(phoneno,null,msg,null,null);
                Toast.makeText(getApplicationContext(),"message has been send to " +phoneno,Toast.LENGTH_SHORT).show();
            }
            catch (Exception ex){
                Toast.makeText(getApplicationContext(),"sms not sent",Toast.LENGTH_SHORT).show();
            }
        }

0 个答案:

没有答案