Start a new activity when app is fresh started and minimized

Viewed 68

First time asking a question, please be polite :)

I want to create an app that when you get an incoming call shows you a message(by starting a new Activity).

I did it, except for one problem. When I first start the app:

1)I have to make a phone call while the app is running upfront in order to work as planned.

2)Then I can minimize it

3)Or even lock the phone and the app is running perfectly

BUT

1)If i start the app

2)Minimize it

3)And then make the phone call nothing happens :(

4)I have to open it , make the phone call and then minimize again to run as it should be.

I am using the android emulator to test it .

MainActivity:

public class MainActivity extends AppCompatActivity
{


    IntentFilter intentFilter;
    CallReceiver callReceiver;


    private void init()
    {
        intentFilter = new IntentFilter();
        callReceiver = new CallReceiver();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        init();



        intentFilter.addAction("android.intent.action.PHONE_STATE");
        intentFilter.addAction("android.intent.action.NEW_OUTGOING_CALL");

        registerReceiver(callReceiver,intentFilter);


    }

CallReceiver:

public class CallReceiver extends PhonecallReceiver
    {

        public static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";


        @Override
        protected void onIncomingCallStarted(Context ctx, String number, Date start)
        {

            openNewActivity(number,ctx);

        }

        @Override
        protected void onOutgoingCallStarted(Context ctx, String number, Date start)
        {
        }

        @Override
        protected void onIncomingCallEnded(Context ctx, String number, Date start, Date end)
        {
        }

        @Override
        protected void onOutgoingCallEnded(Context ctx, String number, Date start, Date end)
        {
        }

        @Override
        protected void onMissedCall(Context ctx, String number, Date missed)
        {
        }



        private void openNewActivity(String number,Context context)
        {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final StringBuilder builder = new StringBuilder();

                    try {
                        
                        Intent i = new Intent(context, IncomingCallActivity.class);
                        String message = "Message I want to be displayed";
                        i.putExtra(EXTRA_MESSAGE, message);


                        final Handler handler = new Handler(Looper.getMainLooper());
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                //Do something after 100ms
                                context.startActivity(i);
                            }
                        }, 1000);


                    } catch (Exception e) {
                        builder.append("Error : ").append(e.getMessage()).append("\n");
                    }

                }
            }).start();
        }
    }

IncomingCallActivity:

public class IncomingCallActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        // Important: have to do the following in order to show without unlocking
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                WindowManager.LayoutParams.FLAG_FULLSCREEN |
                        WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

        setContentView(R.layout.main);

        Intent intent = getIntent();
        String message= intent.getStringExtra(CallReceiver.EXTRA_MESSAGE);

        TextView text = (TextView)findViewById(R.id.text);
        text.setText(message);
    }
}
1 Answers

Android no longer permits launching activity from the background and officialy recommends to show notification instead.

See Restrictions on starting activities from the background.

There are few exceptions, which are mentioned in the docs and explains why it works when your app is in foreground.

I think you should fire a notification that shows the message (or a very simplified UI) you intended to show in your activity and clicking on the notification will open your activity with the full might of your UI.

Related