我正在创建一个通过短信发送当前位置的应用。但是不调用位置监听器方法。它说:
方法从不调用LocationListener
这是我的代码:
public class MainActivity2 extends Activity {
LocationManager lm;
LocationListener locationListener;
private void LocationListener(Context context) {
lm = (LocationManager)
context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
60000,
1000,
locationListener);
}
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
if (loc != null) {
String Uri = "http://maps.google.com/maps?saddr=" + loc.getLatitude() + "," + loc.getLongitude();
SmsManager smsManager = SmsManager.getDefault();
StringBuffer smsBody = new StringBuffer();
smsBody.append(Uri);
smsManager.sendTextMessage("+91847690****", null, smsBody.toString(), null, null);
lm.removeUpdates(locationListener);
}
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main_activity2, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:0)
看起来很混乱的是你在MainActivity2类中创建了一个名为def time = map.get('time') ? it.get('milliseconds') : null
的方法。这不好,因为你不应该有一个与LocationListener类同名的方法。
只需重命名该方法,由于此方法是您的Activity的一部分,因此您无需传入上下文:
LocationListener()
然后,只需从private void setUpLocationListener() {
lm = (LocationManager)
this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
60000,
1000,
locationListener);
}
调用方法:
onCreate()