【问题标题】:Android Activity is Restarting ItselfAndroid Activity 正在重新启动
【发布时间】:2017-01-15 19:52:01
【问题描述】:

我正在制作一个应用程序,其中一个功能是用户位置每 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();
        }

}

【问题讨论】:

  • 我猜是旋转屏幕。
  • 你能详细说明一下吗?
  • “你能详细说明一下吗?” 旋转屏幕代表手机将屏幕方向从横向模式更改为纵向模式时的操作并返回。
  • 没有禁用屏幕旋转。相同的代码在 2 周前运行良好,我认为 android studio 更新搞砸了
  • 旋转屏幕代表手机将屏幕方向从横向模式更改为纵向模式并返回时的操作。我有同样的问题你找到任何解决方案吗?它只发生在像素中

标签: java android android-activity lifecycle google-api-client


【解决方案1】:

请求的权限是否包含在清单中?

这是一个老问题,但是当我遇到同样的问题时我发现了它:

  • 应用在无限循环中不断暂停和恢复,但不会引发异常

在我的情况下,请求权限的行导致了问题。

ActivityCompat.requestPermissions(mParentActivity,
                        new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSION_ACCESS_FINE_LOCATION);

我正在请求 CALL_PHONE 权限,但已将其删除(因为我不再使用它),但没有删除权限检查。两种可能的解决方案:

  • 将请求的权限添加到清单中。

    使用权限 android:name="android.permission.CALL_PHONE"

  • 删除请求权限的行(当然,前提是您不再需要它。)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 2014-01-29
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多