androidonresumestart-activityonpause

Problem of being unable to use other apps when calling intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


In the GpsData app, GPS data is received from the server. The GPS data is received by a thread every second, and the InfoService class sends the number of received GPS data to MainActivity. When checking the logs in MainActivity, onPause() -> onNewIntent() -> onResume() is called.

However, if I press the home button to use other apps, the GpsData app remains open and cannot use other apps.

Is there any way for the GpsData app to continue receiving GPS data while allowing the user to use other apps by pressing the home button?

Code>>

public class Client
{
public class SockHandler extends Handler
    {
        @Override
        public void handleMessage(Message msg)
        {
            switch (msg.what)
            {
                case TCPClient.SOCKET_WRITTEN:
                    break;

                case TCPClient.SOCKET_READ:
                    if (msg.arg1 <= 0)
                        break;

                    byte[]  buf     = (byte[])msg.obj;
                    String  recv    = new String(buf, 0, msg.arg1);

                    mRecvCount += msg.arg1;
                    mListener.setCount(String.format("%d", mRecvCount));

                    break;
                case TCPClient.SOCKET_KEEP_ALIVE:
                    if (!isConnected())     break;`
                    ...                 
                    break;
            }
        }
    }


public interface Message
    {
        void setCount(String count);
    }
}
public class InfoService extends Service implement Client.message {
    
    ........

     @Override
     public void setCount(String count) {
        Intent intent = new Intent(getApplicationContext(),MainActivity.class);
        intent.putExtra("Count", count);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }
}
public class MainActivity extends AppCompatActivity
{

   @Override
    protected void onResume() {
Log.i("MainActivity", "onResume CALL")
        super.onResume();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
    }

    @Override
    protected void onPause() {
Log.i("MainActivity", "onPause CALL")
        super.onPause();
    }
    
   public void setCount(String strCount) {
        mRelayCountView.setText(strCount);
    }


    @Override
    protected void onNewIntent(Intent intent) {
    Log.i("MainActivity", "onNewIntent CALL")
    
    ........    

         if(intent.getStringExtra("Count")!=null) {
                setCount(intent.getStringExtra("Count"));
            }

    .......

        super.onNewIntent(intent);
    }


}
<activity
    android:name=".MainActivity"
    android:label="@string/app_name"
    android:launchMode="singleInstance"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme.NoActionBar"
    android:windowSoftInputMode="stateHidden|adjustPan"></activity>

I couldn't find a solution. Thank you for reading this question.


Solution

  • Is there any way for the GpsData app to continue receiving GPS data while allowing the user to use other apps by pressing the home button?

    Well, you're launching the activity every second from a Service (which seems like something the OS shouldn't even allow) so don't do that.

    The GPS data is received by a thread every second, and the InfoService class sends the number of received GPS data to MainActivity.

    Remove the code that launches the activity, store the value instead in shared preferences or a database and read it later from the activity, and bind to the service from the Activity when the user opens it to receive updates while its running: https://developer.android.com/guide/components/bound-services