服务中的位置更新

时间:2016-06-07 14:41:23

标签: android firebase location firebase-realtime-database

我正在尝试查找用户的位置并更新我的firebase数据库中的值。但该位置似乎没有更新。我无法识别错误。服务已经开始,但位置似乎没有更新。

我的服务如下:

public class MyLocation extends Service {

Firebase rootRef,child_ref;
LocationManager locationManager;
LocationListener locationListener;

public MyLocation() {
    super();

}

@Override
public void onCreate() {
    super.onCreate();
    rootRef = new Firebase(Constants.FIREBASE_URL);
    child_ref = rootRef.child(rootRef.getAuth().getUid());

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {

            child_ref.setValue(location.getLatitude() + " : " + location.getLongitude());
            UserPojo userPojo=new UserPojo(location.getLatitude()+" : "+location.getLongitude());
            child_ref.child("location").setValue(userPojo);
            Log.i("location",location.getLatitude()+" : "+location.getLongitude());

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };
    try {
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
    }
    catch (SecurityException e)
    {
        e.printStackTrace();
    }
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
}

我的POJO课程如下:

public class UserPojo {

String uname;
String email;
String location;


public UserPojo() {
}

public UserPojo(String uname, String email) {
    this.uname = uname;
    this.email = email;

}

public UserPojo(String location) {
    this.location = location;
}

public String getUname() {
    return uname;
}

public String getEmail() {
    return email;
}

public String getLocation() {
    return location;
}
}

我的服务电话活动如下:

public class MembersListActivityFragment extends Fragment {
Intent intent;

private AuthData auth;

public MembersListActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView= inflater.inflate(R.layout.fragment_members_list, container, false);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(new String[]{
                Manifest.permission.INTERNET,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION
        },10);
    }
Firebase firebase=new Firebase(Constants.FIREBASE_URL);
    auth=firebase.getAuth();

    Toast.makeText(getActivity(),auth.getUid(),Toast.LENGTH_LONG).show();




return rootView;
}


private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager)getActivity().getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {

            return true;
        }
    }
    return false;
}







@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode){
        case 10:if(grantResults.length>0&&grantResults[0]== PackageManager.PERMISSION_GRANTED)
        {
           intent=new Intent(getActivity(),MyLocation.class);
            getActivity().startService(intent);

            boolean service_enabled= isMyServiceRunning(MyLocation.class);
            Log.i("service ",service_enabled+"");



        }
    }
}



@Override
public void onDestroy() {
    super.onDestroy();
getActivity().stopService(intent);
}
}

我的清单文件如下:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:name=".Family"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".SignUpActivity" />
    <activity android:name=".SignInAcivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MembersListActivity"
        android:label="@string/title_activity_members_list"
        android:theme="@style/AppTheme.NoActionBar"></activity>
    <service android:name=".MyLocation" android:enabled="true"></service>
</application>

</manifest>

1 个答案:

答案 0 :(得分:0)

您似乎根本没有检查是否有GPS或网络位置服务可用。

还要确保您在清单中拥有正确的权限。

本教程可能有所帮助:http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/