簡體   English   中英

通過短信發送GPS坐標

[英]Sending GPS Coordinates via sms

我是Android新手。 我正在嘗試開發一個可獲取用戶當前GPS坐標並將其通過短信發送到硬編碼號碼的應用。 我正在使用兩個活動,第一個有進度條和一些文本(正在處理,請稍候),它實際上是在后台獲取GPS坐標。 第二個是從第一個活動接收坐標並發送短信。 我面臨的問題是MainActivity.java文件中的條件if(latitude> 0)從不滿足,因為gps通常花費一些時間來獲取坐標,而我無法進行第二次活動。 我正在附上代碼,請為此提供幫助。 您可以根據需要編輯代碼。 提前致謝。

package com.shaji.smartcab;

import com.shaji.smartcab.MainActivity;
import com.shaji.smartcab.MyLocationListener;
import com.shaji.smartcab.R;


import android.os.Bundle;
import android.app.Activity;

import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
import android.location.LocationListener; 
import android.content.Context;
import android.content.Intent;
import android.location.LocationManager;


public class MainActivity extends Activiy{
double lat;
double lon;
String coordinates;
String coor;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TextView tv= (TextView) findViewById(R.id.textView1);



            LocationManager mlocManager=null;  
             LocationListener mlocListener;  
             mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  
             mlocListener = new MyLocationListener();  
            mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 600,10, mlocListener);  



       if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 



                 if(MyLocationListener.latitude>0) {
                lat = MyLocationListener.latitude;
                lon = MyLocationListener.longitude;
                tv.setText("m fetching coordinates");

                coordinates= lat+","+lon;

      Intent intent = new Intent(MainActivity.this, Sec.class);
      intent.putExtra(coor, coordinates);
      startActivity(intent);






                //tv1.setText("Latitude:" + MyLocationListener.latitude +  ",Longitude:" + MyLocationListener.longitude );
                 }}



               else {  
                   Context context = getApplicationContext();
                   CharSequence text = "Please turn ON GPS and Try again Later!";
                   int duration = Toast.LENGTH_SHORT;

                   Toast toast = Toast.makeText(context, text, duration);
                   toast.show(); 
                   tv.setText(" ");

              }  


     };

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}}

enter code here com.shaji.smartcab包;

import android.location.Location;  
import android.location.LocationListener;  
import android.os.Bundle;  

public class MyLocationListener implements LocationListener {  

public static double latitude;  
public static double longitude;

@Override  
public void onLocationChanged(Location loc)  
{  

    loc.getLatitude();  
    loc.getLongitude();  
    latitude=loc.getLatitude();  
    longitude=loc.getLongitude();


}  

@Override  
public void onProviderDisabled(String provider)  
{  
    //print "Currently GPS is Disabled";  
}  
@Override  
public void onProviderEnabled(String provider)  
{  
    //print "GPS got Enabled";  
}  
@Override  
public void onStatusChanged(String provider, int status, Bundle extras)  
{  
}  
} 




//this is the second activity.
package com.shaji.smartcab;
import com.shaji.smartcab.R;

import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class Sec extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);



    final String sms = (String) getIntent().getExtras().get("coor");
    Button c = (Button) findViewById(R.id.button1);
    c.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            String phoneNo = "03136166588";
               //String sms = "coordinates";

               try {
                                       SmsManager smsManager = SmsManager.getDefault();
                                        smsManager.sendTextMessage(phoneNo, null, sms, null, null);
                                        Toast.makeText(getApplicationContext(), "Request Sent!",
                                                Toast.LENGTH_LONG).show();
                                    } catch (Exception e) {
                                        Toast.makeText(getApplicationContext(),
                                                " Error, please try again later!",
                                                Toast.LENGTH_LONG).show();
                                        e.printStackTrace();

        }

        }});


}}

我認為問題在於,在調用下一個活動之前,位置更新尚未完成。

在獲取更新之前,請使用3個提供者之一獲取坐標。 請參考以下代碼。

private Location getUserLocation() {

location_manager = (LocationManager) getSystemService(LOCATION_SERVICE);
if (location_manager != null) {

   List<String > provider=location_manager .getAllProviders();
            Location location = null;
            for(int i=0;i<provider.size();i++){
                 location=location_manager .getLastKnownLocation(provider.get(i));
                 if(location!=null){
                          location_manager.requestLocationUpdates(provider.get(i),
                                                    MIN_TIME_BW_UPDATES,
                                                    MIN_DISTANCE_CHANGE_FOR_UPDATES,mlocListener);
                       break;
                      }
            }


return location;

}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM