【问题标题】:Start a new activity when app is fresh started and minimized当应用程序重新启动并最小化时启动新活动
【发布时间】:2021-06-11 22:32:05
【问题描述】:

第一次提问,请客气:)

我想创建一个应用,当您接到来电时会向您显示一条消息(通过启动一个新 Activity)。

我做到了,除了一个问题。当我第一次启动应用程序时:

1)我必须在应用程序运行前打个电话才能按计划工作。

2)然后我可以最小化它

3)甚至锁定手机,应用程序运行完美

但是

1)如果我启动应用程序

2) 最小化它

3)然后打电话什么都没有发生 :(

4)我必须打开它,拨打电话,然后再次最小化才能正常运行。

我是用安卓模拟器测试的。

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

【问题讨论】:

    标签: android android-activity broadcastreceiver phone-call


    【解决方案1】:

    Android 不再允许从后台启动 Activity,官方建议改为显示通知。

    Restrictions on starting activities from the background

    文档中提到了一些例外情况,并解释了为什么它在您的应用处于前台时有效。

    我认为您应该触发一个通知,显示您打算在您的活动中显示的消息(或非常简化的 UI),然后单击通知将打开您的活动,并充分利用您的 UI。

    【讨论】:

    • 感谢您的回答,非常有用。正如文档中提到的那样,我将使用通知解决方案。但是您能否更具体地了解触发一个非常简化的 UI。什么样的用户界面?我的主要目标是在接到来电时尽可能提醒用户(使用任何类型的消息)。
    • 如果是文本消息,那就更简单了——只需触发标准通知即可。选择标题和描述并启动它。见developer.android.com/guide/topics/ui/notifiers/notifications
    猜你喜欢
    • 2014-09-11
    • 2011-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多