我有和活动开始IntentService
,应该定期更新该位置并将其发布到Firebase。不知怎的,它不起作用。没有例外,没有错误或任何事情。它只是没有做我想要的。
这是我的主要活动:
public class MainActivity extends AppCompatActivity {
private Intent mTrackingIntent;
//UI
protected Button mStartTrackingButton;
protected Button mStopTrackingButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Locate the UI widgets
mStartTrackingButton = (Button) findViewById(R.id.start_button);
mStopTrackingButton = (Button) findViewById(R.id.stop_button);
mTrackingIntent = new Intent(this, TrackingService.class);
}
public void startButtonHandler(View view) {
startService(mTrackingIntent);
}
public void stopButtonHandler(View view) {
stopService(mTrackingIntent);
}
以下是TrackingService
:
public class TrackingService extends IntentService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private FirebaseAuth mFirebaseAuth;
private FirebaseUser mFirebaseUser;
public static final String SENDER_EMAIL = "*************";
public static final String SENDER_PASSWORD = "******************";
private DatabaseReference mDatabase;
private String mUserId;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 10000; //upper bound
public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2; //lower bound
private String mLastUpdateTime;
private String mLastUpdateYear;
private String mLastUpdateMonth;
private String mLastUpdateDay;
private static final SimpleDateFormat mYearFormat = new SimpleDateFormat("yyyy");
private static final SimpleDateFormat mMonthFormat = new SimpleDateFormat("MM");
private static final SimpleDateFormat mDayFormat = new SimpleDateFormat("dd");
private static final SimpleDateFormat mTimeFormat = new SimpleDateFormat("HH:mm:ss");
private Location mLastUpdateLocation;
public TrackingService() {
super("TrackingService");
}
@Override
public void onDestroy() {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
super.onDestroy();
}
@Override
protected void onHandleIntent(Intent intent) {
//Initialize Firebase Auth
initFirebase();
//connect to the Google API
buildGoogleApiClient();
startLocationUpdates();
}
private void initFirebase() {
mFirebaseAuth = FirebaseAuth.getInstance();
mFirebaseUser = mFirebaseAuth.getCurrentUser();
mDatabase = FirebaseDatabase.getInstance().getReference();
if (mFirebaseUser == null) {
//log in to Firebase
mFirebaseAuth.signInWithEmailAndPassword(SENDER_EMAIL, SENDER_PASSWORD);
mFirebaseUser = mFirebaseAuth.getCurrentUser();
mUserId = mFirebaseUser.getUid();
}
mUserId = mFirebaseUser.getUid();
}
protected void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
//Set update interval bounds and accuracy
createLocationRequest();
mGoogleApiClient.connect();
}
private void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void startLocationUpdates() {
//check if permission to get the location is granted
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
@Override
public void onLocationChanged(Location location) {
Date updateDate = Calendar.getInstance().getTime();
mLastUpdateTime = mTimeFormat.format(updateDate);
mLastUpdateYear = mYearFormat.format(updateDate);
mLastUpdateMonth = mMonthFormat.format(updateDate);
mLastUpdateDay = mDayFormat.format(updateDate);
mLastUpdateLocation = location;
//post to Firebase
postToFirebase(location);
}
public void postToFirebase(Location location) {
mDatabase.child("users").child(mUserId).child(mLastUpdateYear).child(mLastUpdateMonth).child(mLastUpdateDay).child("trackRecords").push().setValue(location);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
}
答案 0 :(得分:1)
IntentService并不是这项工作的正确工具。 The way IntentService works就是这个。当从startService()收到Intent时,它将使用后台线程来处理intent(从onHandleIntent()开始),当该方法返回时,如果队列中没有其他意图,IntentService将自行停止并关闭
相反,您可能需要创建一个manages its lifecycle的自定义服务,以便它执行其后台工作,以便它保持活动状态,直到它被告知停止。