Problem occurs while sending location via message2019 Community Moderator ElectionAndroid AlarmManager for periodical sensor readingWriting my own exception classI am trying to set alarm on specific time using alarm manager but alarm initiated instantly?Unable to see multiple marker on google MapsetText on button from another activity androidSaving ToggleButton state in ListView by using SharedPreferencesPermission granted in app by user Android MLogin activity with volley, php, mysql after login success intent not go to other activityProblem with Location Manager/Listener on my Android appi am trying to get my current location, but this shows wrong. And I also try to get my location using the following code, but it's not working

How to detect if C code (which needs 'extern C') is compiled in C++

Reversed Sudoku

What are some noteworthy "mic-drop" moments in math?

Find longest word in a string: are any of these algorithms good?

Examples of a statistic that is not independent of sample's distribution?

Error during using callback start_page_number in lualatex

Can I pump my MTB tire to max (55 psi / 380 kPa) without the tube inside bursting?

School performs periodic password audits. Is my password compromised?

Solar Charging Car Battery through Cigarette Lighter Outlet?

What was the implant device Captain Marvel was using?

Marriage green card at end of current visa with 2 Year residency requirement waiver in-process, question

How can I get players to stop ignoring or overlooking the plot hooks I'm giving them?

An alternative proof of an application of Hahn-Banach

Professor expects me to attend a conference, I can't afford even with 50% funding

How to secure an aircraft at a transient parking space?

Is this Paypal Github SDK reference really a dangerous site?

How does NOW work?

Hotkey (or other quick way) to insert a keyframe for only one component of a vector-valued property?

What would be the most expensive material to an intergalactic society?

What wound would be of little consequence to a biped but terrible for a quadruped?

How do I express some one as a black person?

Should I tell my boss the work he did was worthless

PTIJ: Should I kill my computer after installing software?

cat shows nothing



Problem occurs while sending location via message



2019 Community Moderator ElectionAndroid AlarmManager for periodical sensor readingWriting my own exception classI am trying to set alarm on specific time using alarm manager but alarm initiated instantly?Unable to see multiple marker on google MapsetText on button from another activity androidSaving ToggleButton state in ListView by using SharedPreferencesPermission granted in app by user Android MLogin activity with volley, php, mysql after login success intent not go to other activityProblem with Location Manager/Listener on my Android appi am trying to get my current location, but this shows wrong. And I also try to get my location using the following code, but it's not working










1















I want to send the location via message but sometimes it sends the message, sometimes it sends the null string and sometimes it does not send.




This is the button click on which I want to send the message to the
Recipient using the current location




 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);





This is just to show the message of the current location and the mobile
numbers
for testing



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


The SMS(maybe due to exceeding the message limit )




 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;



This is for requesting the permission




 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);







This is the function for sms sending




 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();











share|improve this question
























  • Did i post the code in neat format or should i change it, its my first time so no knowledge please comment if i am wrong

    – Sagar Acharya
    Mar 8 at 9:06















1















I want to send the location via message but sometimes it sends the message, sometimes it sends the null string and sometimes it does not send.




This is the button click on which I want to send the message to the
Recipient using the current location




 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);





This is just to show the message of the current location and the mobile
numbers
for testing



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


The SMS(maybe due to exceeding the message limit )




 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;



This is for requesting the permission




 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);







This is the function for sms sending




 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();











share|improve this question
























  • Did i post the code in neat format or should i change it, its my first time so no knowledge please comment if i am wrong

    – Sagar Acharya
    Mar 8 at 9:06













1












1








1








I want to send the location via message but sometimes it sends the message, sometimes it sends the null string and sometimes it does not send.




This is the button click on which I want to send the message to the
Recipient using the current location




 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);





This is just to show the message of the current location and the mobile
numbers
for testing



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


The SMS(maybe due to exceeding the message limit )




 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;



This is for requesting the permission




 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);







This is the function for sms sending




 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();











share|improve this question
















I want to send the location via message but sometimes it sends the message, sometimes it sends the null string and sometimes it does not send.




This is the button click on which I want to send the message to the
Recipient using the current location




 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);





This is just to show the message of the current location and the mobile
numbers
for testing



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


The SMS(maybe due to exceeding the message limit )




 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;



This is for requesting the permission




 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);







This is the function for sms sending




 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();








java android location sms geocoder






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 8:59









Sabbir Ahmed

1615




1615










asked Mar 7 at 6:24









Sagar AcharyaSagar Acharya

62




62












  • Did i post the code in neat format or should i change it, its my first time so no knowledge please comment if i am wrong

    – Sagar Acharya
    Mar 8 at 9:06

















  • Did i post the code in neat format or should i change it, its my first time so no knowledge please comment if i am wrong

    – Sagar Acharya
    Mar 8 at 9:06
















Did i post the code in neat format or should i change it, its my first time so no knowledge please comment if i am wrong

– Sagar Acharya
Mar 8 at 9:06





Did i post the code in neat format or should i change it, its my first time so no knowledge please comment if i am wrong

– Sagar Acharya
Mar 8 at 9:06












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55037303%2fproblem-occurs-while-sending-location-via-message%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55037303%2fproblem-occurs-while-sending-location-via-message%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229