我是android新手。我正在尝试开发一个应用程序,用于获取用户当前的GPS坐标并通过短信将其发送到硬编码的数字。我正在使用两个活动,第一个有进度条和一些文本(Processing,Please wait),它实际上是在后台获取GPS坐标。第二个是从第一个活动接收坐标并发送短信。我面临的问题是,在MainActivity.java文件中,条件if(纬度> 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();
}
}});
}}
答案 0 :(得分:0)
我认为问题是,在您调用下一个Activity之前,不会进行位置更新。
在获得更新之前,请使用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;
}
}