Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Activity is Restarting Itself

I am making an app in which one of the features, the users location is updated once every 10 seconds. So far the app is not updating it's location because the activity is paused and restarted before the location is retrieved. I don't understand why the activity is being restarted. My best guess is, since there is not a whole lot going on, the OS is pausing the activity, but what will trigger the activity to restart? I have attempted to built the app before, and then I restarted from scratch because I made too many mistakes but even then, I have not seen this problem before.

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

Here is the class I made to update the location automatically:

    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
            }

        }

    }

Here is the main activity code:

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();
        }

}
like image 987
law Avatar asked Jan 15 '17 19:01

law


1 Answers

Is the requested permission included in the manifest?

This is an old question, but I found it when I ran into the same problem:

  • App keeps pausing and resuming, in an endless loop, but doesn't throw an exception

In my case, the line where permissions were requested was causing the problem.

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

I was requesting the CALL_PHONE permission, but had removed it (because I wasn't using it anymore), but did not remove the permission check. Two possible solutions:

  • Add the requested permission to the manifest.

    uses-permission android:name="android.permission.CALL_PHONE"

  • Remove the line requesting the permission (only if you don't need it anymore, of course.)

like image 170
Arno Avatar answered Sep 21 '22 03:09

Arno