从不调用LocationManager.requestLocationUpdates

时间:2016-04-28 14:22:13

标签: android location locationmanager

我有一个应用程序,我想使用GPS的位置。实现我的教学后,我有两个文件,VirtualAssistantActivity使用LocationGPS文件。第二个文件将使用从第一个文件接收的意图发送结果。

问题是在收到TextView填充的意图后,应该编辑的location.toString()不会改变。在使用Android Studio的调试功能后,我发现从未调用locationManager的{​​{1}},我甚至无法进入新requestLocationUpdate的创建

我正在使用运行Android 5.0的Sony XPERIA,并决定使用API​​ 15开发目标Android 4.0。

这是VirtualAssistantActivity代码:

LocationListener

和LocationGPS的:

public class VirtualAssistantActivity extends AppCompatActivity {

private LocationGPS lgps = null;
private BroadcastReceiver mLocationReceiver;
private TextView tvTest = null;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_virtual_assistant);

    tvTest = (TextView) findViewById(R.id.tV_testGeo);
    lgps = LocationGPS.get(this.getApplicationContext());
    mLocationReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent){
            Location l = (Location)intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED);
            if (l!=null){
                tvTest.setText(""+l);
            } else {
                tvTest.setText("Empty intent");
            }
        }
    };

}@Override
protected void onResume(){
    super.onResume();
    this.registerReceiver(mLocationReceiver, new IntentFilter(LocationGPS.ACTION_LOCATION));
    lgps.startLocationUpdates();
    Toast.makeText(getApplicationContext(), "BR Init", Toast.LENGTH_SHORT).show();
}

@Override
public void onPause() {
    super.onPause();
    //unregisterReceiver(mLocationReceiver);
}

我已经搜索了一些解决方案,虽然它似乎适用于所有人,或者至少它们有一些错误消息,但我不知道除了最终使用Google的API之外我能做什么,但我宁愿避免那。我已经检查了我的许可,他们被授予,这是我的进口清单:

public class LocationGPS {

static LocationGPS slocationGPS = null;
Context mAppContext = null;
LocationManager mLocationManager = null;

public static String ACTION_LOCATION = "LOCATION_MSG";

private LocationGPS(Context appContext) {
    mAppContext = appContext;
    mLocationManager = (LocationManager)appContext.getSystemService(Context.LOCATION_SERVICE);
}

public static LocationGPS get(Context c) {
    if (slocationGPS == null) {
        slocationGPS = new LocationGPS(c);
    }
    return slocationGPS;
}

private void broadcastLocation(Location location) {
    Intent broadcast = new Intent(this.ACTION_LOCATION);
    broadcast.putExtra(LocationManager.KEY_LOCATION_CHANGED, location);
    this.mAppContext.sendBroadcast(broadcast);
}

public void startLocationUpdates(){
    if (ContextCompat.checkSelfPermission(mAppContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(mAppContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    mLocationManager.requestLocationUpdates
            (LocationManager.GPS_PROVIDER, 1000, 5, new LocationListener() {
                @Override
                public void onLocationChanged(Location location) {
                    if (location != null) {
                        //Log.d(this.getClass().getName(), location.toString());
                        Log.d("GPS", "Latitude " + location.getLatitude() + " et longitude " + location.getLongitude());
                        Toast.makeText(mAppContext, "Location", Toast.LENGTH_SHORT).show();
                        broadcastLocation(location);
                    }
                    Toast.makeText(mAppContext, "Location", Toast.LENGTH_SHORT).show();
                }
                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
                }
                @Override
                public void onProviderEnabled(String provider) {
                }
                @Override
                public void onProviderDisabled(String provider) {

                }
            });
}

和权限:

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager; 
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.widget.Toast;

0 个答案:

没有答案