我是安卓新手。我正在尝试开发一个应用程序,它可以获取用户当前的 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();
}
}});
}}