通过短信发送GPS坐标 [英] Sending GPS Coordinates via sms

查看:146
本文介绍了通过短信发送GPS坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android新手。我试图开发一个应用程序,获取用户当前的GPS坐标,并通过短信发送到硬编码的数字。我正在使用两项活动,第一项有一个进度条和一些文字(处理,请稍候),它实际上是在后台获取GPS坐标。第二个是从第一个活动接收坐标并发送短信。我面临的问题是,在MainActivity.java文件中,条件if(latitude> 0)永远不会被满足,因为gps通常需要一些时间才能获得坐标,而我不能转到第二个活动。我附上代码,请帮助我。如果你喜欢,你可以编辑代码。
感谢提前。

I am new to android. i am trying to develop an app which fetch users current GPS coordinates and send them via sms to a hardcoded number. I am using two activities, first one has a progress bar and some text( Processing, Please wait),it is actually fetching GPS coordinates in background. The second one is receiving coordinates from first activity and sending the sms. The problem which i m facing is that in MainActivity.java file the condition if(latitude>0) is never satisfied since gps normally take some time to get coordinates and I cant go to second activity. I m attaching the code, please help me on this. you can edit the code if u like. Thanks in Advance.

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

在此输入代码 package com.shaji.smartcab;

enter code herepackage 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();

        }

        }});


}}


推荐答案

我认为问题在于,在您调用下一个Activity之前,位置更新没有完成。

I think the problem is, location update is not done before you call the next Activity.

在获取更新之前,请获取坐标这3家提供商。

Before you get the updates, get the co-ordinates using one of the 3 providers. Refer following code.

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;

}
}

这篇关于通过短信发送GPS坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆