我想在几秒钟后获得用户的经度和纬度。 runnable应该重复该方法几秒钟。如果获得经度和纬度,则应该停止运行。
public class LocationActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;
ProgressDialog pd;
ArrayList<String> carModelAL;
Spinner spinner;
SharedPreferences sharedPreferences;
String CAR_MODEL_URL = "http://192.168.0.195:85/tranxavApi/public/api/get-car-model";
String modelValue;
Button btn;
Intent intent;
TextView detail, selectModel;
final static Handler handler = null;
Runnable runnable;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length >0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,0, locationListener);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
final Handler handler = new Handler();
detail = (TextView) findViewById(R.id.detail);
Typeface type = Typeface.createFromAsset(getAssets(),"fonts/RobotoCondensedBold.ttf");
detail.setTypeface(type);
selectModel = (TextView) findViewById(R.id.selectModel);
Typeface typeModel = Typeface.createFromAsset(getAssets(),"fonts/RobotoCondensedBold.ttf");
detail.setTypeface(typeModel);
carModelAL = new ArrayList<String>();
spinner = (Spinner) findViewById(R.id.carModelSP);
spinner.setAdapter(new ArrayAdapter<String>(LocationActivity.this, android.R.layout.simple_list_item_1, carModelAL));
getCarModel(CAR_MODEL_URL);
sharedPreferences = getApplication().getSharedPreferences("myPref", MODE_PRIVATE);
String likelyProblem = sharedPreferences.getString("likelyProblem","");
Log.i("info", likelyProblem);
btn = (Button)findViewById(R.id.detailBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pd = new ProgressDialog(LocationActivity.this);
pd.setMessage("\tPlease Wait...");
pd.setCancelable(false);
pd.show();
sharedPreferences = getApplication().getSharedPreferences("myPref", MODE_PRIVATE);
if (sharedPreferences.contains("email") && sharedPreferences.contains("likelyProblem")) {
runnable = new Runnable() {
@Override
public void run() {
String modelSelected = spinner.getSelectedItem().toString();
String email = sharedPreferences.getString("email","");
String likelyProblem = sharedPreferences.getString("likelyProblem","");
Log.i("info", likelyProblem);
latAndLong(modelSelected, email, likelyProblem);
handler.postDelayed(this, 3000);
}
};
handler.post(runnable);
}
}
});
}
private void latAndLong(final String modelSelected, final String email, final String likelyProblem){
ActivityCompat.requestPermissions(LocationActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
GeoLocation geoLocation = new GeoLocation(getApplicationContext());
Location l = geoLocation.getLocation();
if (l != null){
handler.removeCallbacks(runnable);
pd.dismiss();
double lat = l.getLatitude();
double lng = l.getLongitude();
Toast.makeText(getApplicationContext(), "Lat: " + lat + "\n Lon: " + lng, Toast.LENGTH_LONG).show();
LocationSender locationSender = new LocationSender(lat, lng, email, modelSelected, likelyProblem);
sendNetworkRequest(locationSender);
AlertDialog alertDialog = new AlertDialog.Builder(LocationActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Message has been sent to the mechanic he will be there in 5 mins. After he finishes click procced to continue.");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Proceed >>>",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// dialog.dismiss();
intent = new Intent(LocationActivity.this, ProblemsActivity.class);
startActivity(intent);
}
});
alertDialog.show();
}
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
if (Build.VERSION.SDK_INT < 23){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}else{
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}else{
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
}
日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.system2.tranxav, PID: 29069
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Handler.removeCallbacks(java.lang.Runnable)' on a null object reference
at com.example.system2.tranxav.LocationActivity.latAndLong(LocationActivity.java:150)
at com.example.system2.tranxav.LocationActivity.access$000(LocationActivity.java:56)
at com.example.system2.tranxav.LocationActivity$1$1.run(LocationActivity.java:131)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
我已尝试按照此站点的描述使处理程序保持静态
{{3}}
但它不起作用。任何帮助将非常感谢
答案 0 :(得分:0)
我已经解决了这个问题。我在oncreate方法中而不是在类
中声明了处理程序