【问题标题】:Start Activity in a BroadcastReceiver在 BroadcastReceiver 中启动 Activity
【发布时间】:2017-03-12 01:43:30
【问题描述】:

我已经构建了一个小应用程序。它唯一能做的就是接听一个拨出电话并在它发生时显示一些活动。只有一个Activity 和一个BroadcastReceiver

我想将我的代码与另一个应用程序集成,我从 Manifest.xml 中删除了 BroadcastReceiver,并从主要活动中动态创建(并注册)它。我的接收器开火良好,但没有显示活动。

这两种方法有什么区别?

如何让活动显示出来?

来自MainActivity.java

callInterceptor = new InterceptOutgoingCall();
IntentFilter callInterceptorIntentFilter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL");
callInterceptorIntentFilter.setPriority(100);
registerReceiver(callInterceptor,  callInterceptorIntentFilter);

从函数receiver.onReceive(Context,Intent):

Intent alertIntent = new Intent(context, AlertActivity.class);
alertIntent.putExtra("callnumber", phonenbr);
alertIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alertIntent);

我的活动在manifest 中声明如下:

<activity android:name=".AlertActivity" 
            android:screenOrientation="portrait"/>

【问题讨论】:

    标签: android android-activity broadcastreceiver


    【解决方案1】:

    我在两个线程中找到了答案:

    1. Android launch an activity from a broadcast receiver

    2. Activity started from notification opened on top of the activity stack

    在清单中,应使用android:taskAffinity 声明活动。 在开始意图时,我必须添加一个标志 = Intent.FLAG_ACTIVITY_NEW_TASK

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      相关资源
      最近更新 更多