我正在创建一个应用程序,其中一个功能,用户位置每10秒更新一次。到目前为止,应用程序没有更新它的位置,因为活动在检索位置之前暂停并重新启动。我不明白为什么重启活动。我最好的猜测是,由于没有很多事情发生,操作系统暂停活动,但是什么会触发活动重启?我之前试图构建应用程序,然后我从头开始重新启动,因为我犯了太多错误,但即便如此,我还没有看到过这个问题。
D/selectRouteAndTransportMethod: On Start
D/myLocation: Connection Requested
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/myLocation: Connection Requested
V/FA: Activity resumed, time: 8562145
V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 32
V/FA: Activity paused, time: 8562177
V/FA: onActivityCreated
D/Activity: performCreate Call secproduct feature valuefalse
D/Activity: performCreate Call debug elastic valuetrue
D/selectRouteAndTransportMethod: On Start
D/myLocation: Connection Requested
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
D/myLocation: Connection Requested
V/FA: Activity resumed, time: 8562374
D/myLocation: OnConnected
D/myLocation: Permission Was Granted
V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 162
V/FA: Activity paused, time: 8562535
D/myLocation: OnConnected
D/myLocation: Permission Was Granted
V/FA: onActivityCreated
D/Activity: performCreate Call secproduct feature valuefalse
D/Activity: performCreate Call debug elastic valuetrue
D/selectRouteAndTransportMethod: On Start
D/myLocation: Connection Requested
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
V/FA: Activity resumed, time: 8562971
D/myLocation: Connection Requested
V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 20
V/FA: Activity paused, time: 8562991
V/FA: onActivityCreated
D/Activity: performCreate Call secproduct feature valuefalse
D/Activity: performCreate Call debug elastic valuetrue
D/selectRouteAndTransportMethod: On Start
D/myLocation: Connection Requested
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
V/FA: Activity resumed, time: 8563169
D/myLocation: Connection Requested
D/myLocation: OnConnected
D/myLocation: Permission Was Granted
I/art: Background partial concurrent mark sweep GC freed 25697(1746KB) AllocSpace objects, 6(96KB) LOS objects, 33% free, 31MB/47MB, paused 3.173ms total 176.879ms
V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 171
V/FA: Activity paused, time: 8563339
D/myLocation: OnConnected
D/myLocation: Permission Was Granted
V/FA: onActivityCreated
D/Activity: performCreate Call secproduct feature valuefalse
D/Activity: performCreate Call debug elastic valuetrue
D/selectRouteAndTransportMethod: On Start
D/myLocation: Connection Requested
D/selectRouteAndTransportMethod: Connecting
D/selectRouteAndTransportMethod: registered
V/FA: Activity resumed, time: 8563432
D/myLocation: Connection Requested
D/myLocation: OnConnected
D/myLocation: Permission Was Granted
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@21fc22ec time:3722364
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@39b2d053 time:3722365
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2c7d74f4 time:3722366
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@14c588fb time:3722366
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@447b885 time:3722366
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@32c680a3 time:3722367
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@3948204 time:3722367
V/ActivityThread: updateVisibility : ActivityRecord{252a28bb token=android.os.BinderProxy@247ca0d3 {user.com.commuterapp/user.com.commuterapp.selectRouteAndTransportMethod}} show : false
V/ActivityThread: updateVisibility : ActivityRecord{26fe81d8 token=android.os.BinderProxy@3948204 {user.com.commuterapp/user.com.commuterapp.selectRouteAndTransportMethod}} show : false
V/ActivityThread: updateVisibility : ActivityRecord{2afd4931 token=android.os.BinderProxy@32c680a3 {user.com.commuterapp/user.com.commuterapp.selectRouteAndTransportMethod}} show : false
V/ActivityThread: updateVisibility : ActivityRecord{70b4816 token=android.os.BinderProxy@447b885 {user.com.commuterapp/user.com.commuterapp.selectRouteAndTransportMethod}} show : false
V/ActivityThread: updateVisibility : ActivityRecord{6c89597 token=android.os.BinderProxy@14c588fb {user.com.commuterapp/user.com.commuterapp.selectRouteAndTransportMethod}} show : false
V/ActivityThread: updateVisibility : ActivityRecord{ac5e384 token=android.os.BinderPr
以下是我自动更新位置的课程:
package user.com.commuterapp;
import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.content.pm.PackageManager;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.common.ConnectionResult;
import android.location.Location;
import android.content.IntentSender;
import android.util.Log;
import android.support.v4.content.ContextCompat;
import android.content.Context;
/**
* Created by XXX XXXXX on 11/27/2016.
*
* Function: A simple interface that utilizes Google API CLient to update the users location
* once every 15 seconds. Used when the user wants the select his/her current location as his travel
* origin.
*
* This class implements the Google API Client Connection Callbacks to async process successful connection requests
* This class implements the Google API Client onConnectionFailedListener to async process failed connec
tion requests
* This class implements the Google API Client onRequestPermissionResultCallback to async process user permission requests
* for location updtaes
*/
public class myLocation implements
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
ActivityCompat.OnRequestPermissionsResultCallback,
LocationListener {
private final static String TAG = "myLocation"; // for debugging purposes, to tell user the origin class for debug information
private final static int MY_PERMISSION_ACCESS_FINE_LOCATION = 1;
private final static long ONE_SECOND = 1000;
private final static long UPDATE_INTERVAL = ONE_SECOND;
private final static long FASTEST_UPDATE_INTERVAL = ONE_SECOND;
public GoogleApiClient mGoogleApiClient = null; //Google API Client object
public Location mCurrentLocation; //current location of phone
private Context mParentBase; //Global information on app environment from parent class to be used to create Google API Client Object
private Activity mParentActivity; //Contains parent classes information on presentation layer
private PendingIntent mParentIntent; //Passes on the rights to the application to another activity
private LocationRequest requestLocation; //Object that contains auto location update paramaters
/**
* Constructor for my location, recieves calling classes Conxtext, Intent and Activity
* Information
*
* @param Base
* @param activitiyBase
* @param intent
*/
public myLocation(Context Base, Activity activitiyBase, PendingIntent intent) {
mParentBase = Base;
mParentActivity = activitiyBase;
mParentIntent = intent;
initialLocationService();
}
/**
* Public class to initiate connection to the API
*/
public void connect() {
mGoogleApiClient.connect();
Log.d(TAG, "Connection Requested");
}
/**
* Override public class from Google API Connection Callback,
* Called when application has connected to the API.
* @param bundle
*/
@Override
public void onConnected(@Nullable Bundle bundle) {
Log.d(TAG, "OnConnected");
if (PackageManager.PERMISSION_GRANTED ==
(ContextCompat.checkSelfPermission(mParentBase,
android.Manifest.permission.ACCESS_FINE_LOCATION)))
{
Log.d(TAG, "Permission Was Granted");
//mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, requestLocation, mParentIntent);
if (mCurrentLocation == null) {
} else {
Log.d(TAG, mCurrentLocation.toString());
}
} else {
ActivityCompat.requestPermissions(mParentActivity,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_ACCESS_FINE_LOCATION);
Log.d(TAG, "Permission Was Requested");
}
}
/**
* Override from GoogleAPI connection override failed listener. Used when the connection o the API
* has failed
* @param connectionResult
*/
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.i(TAG, "Connection Failed");
// just logging for now
}
/**
* Not added, but will be used when permission to access phone location is not granted
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions, @NonNull int[] grantResults) {
// loading from Android Studio, not going to worry about this now
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Connection Suspended");
// just logging for now, not trying to resolve failure
}
/**
* Callback for when a new location is recieved
* @param location
*/
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, "Entered the onLocationChanged() method");
// this calls one or the other private methods that don't
// matter to your problem. I can post them if you like.
if (location != null) {
Log.d(TAG, location.toString());
mCurrentLocation = location;
} else {
Log.d(TAG, "Can't get last location");
}
}
/**
* Initializes new Google API Client and Automatic Location Updates
*/
private void initialLocationService() {
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(mParentBase)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
requestLocation = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(1 * 1000) // 15 seconds, in milliseconds
.setFastestInterval(1 * 1000); // 15 second, in milliseconds
}
}
}
以下是主要活动代码:
package user.com.commuterapp;
import android.app.PendingIntent;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import user.com.commuterapp.R;
import static java.lang.Boolean.TRUE;
public class selectRouteAndTransportMethod extends AppCompatActivity {
Intent mIntent;
PendingIntent mPendingIntent;
myLocation mCurrentLocation;
public static final String TAG = selectRouteAndTransportMethod.class.getSimpleName();
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_route_and_transport_method);
mIntent = new Intent(this, selectRouteAndTransportMethod.class);
mPendingIntent = PendingIntent.getActivity(this, 0, mIntent, 0);
mCurrentLocation = new myLocation(this, this, mPendingIntent);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "On Start");
mCurrentLocation.connect();
boolean connecting = mCurrentLocation.mGoogleApiClient.isConnecting();
boolean registered = mCurrentLocation.mGoogleApiClient.isConnectionCallbacksRegistered(mCurrentLocation);
//ConnectionResult connectionResult = mCurrentLocation.mGoogleApiClient.getConnectionResult(LocationServices.API);
if (connecting == TRUE) {
Log.d(TAG, "Connecting");
}
if (registered == TRUE) {
Log.d(TAG, "registered");
}
}
@Override
protected void onResume() {
super.onResume();
mCurrentLocation.connect();
}
@Override
protected void onPause() {
super.onPause();
// mCurrentLocation.disconnect();
}
}
答案 0 :(得分:0)
清单中是否包含请求的许可?
这是一个古老的问题,但是当我遇到相同的问题时我发现了它:
就我而言,请求权限的行引起了问题。
ActivityCompat.requestPermissions(mParentActivity,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSION_ACCESS_FINE_LOCATION);
我正在请求CALL_PHONE权限,但是已将其删除(因为我不再使用它了),但是没有删除权限检查。两种可能的解决方案:
向清单添加请求的权限。
uses-permission android:name =“ android.permission.CALL_PHONE”
删除请求许可的行(当然,仅在不再需要该行的情况下。)