0
我試圖找到用戶的位置並更新我的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>
我已經添加了必需的權限,可以在manifest文件 –
這個「教程」不會幫助!!!! 'GPSTracker擴展Service',然後'gps = new GPSTracker(this);'**違背了基本的Android規則**(即:**不要**用new運算符自己創建Services,Activities,Applications類的實例) – Selvin