更新
我能解决这个问题, 问题是对话框和相关可以从广播接收器发起但不推荐,因为活动在视图之前结束。
尝试实现覆盖,如fb messenger,truecaller等。
public class IncomingCall extends BroadcastReceiver
{
private Context pcontext;
private static final String TAG = "CustomBroadcastReceiver";
TelephonyManager telephony;
CustomPhoneStateListener customPhoneListener ;
@Override
public void onReceive(Context context, Intent intent)
{
pcontext = context;
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
Log.w("DEBUG", state);
telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
customPhoneListener = new CustomPhoneStateListener();
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Bundle bundle = intent.getExtras();
String phoneNr= bundle.getString("incoming_number");
}
}
public class CustomPhoneStateListener extends PhoneStateListener
{
private static final String TAG = "CustomPhoneStateListener";
Handler handler=new Handler();
@Override
public void onCallStateChanged(int state, String incomingNumber)
{
WindowManager wm = (WindowManager) pcontext.getSystemService(Context.WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT |
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSPARENT);
params.height = LayoutParams.MATCH_PARENT;
params.width = LayoutParams.MATCH_PARENT;
params.format = PixelFormat.TRANSLUCENT;
params.gravity = Gravity.BOTTOM;
RelativeLayout ly;
final LayoutInflater inflater = (LayoutInflater) pcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ly = (RelativeLayout) inflater.inflate(R.layout.dialog, null);
switch (state)
{
case TelephonyManager.CALL_STATE_RINGING:
Log.d("Call","RINGING");
wm.addView(ly, params);
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.d("Call","End");
//WindowManager wm = (WindowManager) pcontext.getSystemService(Context.WINDOW_SERVICE);
if(ly!=null)
{
wm.removeView(ly);
ly = null;
}
break;
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_NONE);
}
}
}
addView工作正常, 这是日志
“查看未附加到窗口管理器”
08-24 20:05:56.404: W/DEBUG(28001): IDLE
08-24 20:05:56.424: D/Call(28001): End
08-24 20:05:56.424: D/AndroidRuntime(28001): Shutting down VM
08-24 20:05:56.424: W/dalvikvm(28001): threadid=1: thread exiting with uncaught exception (group=0x412982a0)
08-24 20:05:56.444: E/AndroidRuntime(28001): FATAL EXCEPTION: main
08-24 20:05:56.444: E/AndroidRuntime(28001): java.lang.IllegalArgumentException: View not attached to window manager
08-24 20:05:56.444: E/AndroidRuntime(28001): at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:673)
08-24 20:05:56.444: E/AndroidRuntime(28001): at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:369)
08-24 20:05:56.444: E/AndroidRuntime(28001): at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:160)
08-24 20:05:56.444: E/AndroidRuntime(28001): at com.androidexample.broadcastreceiver.IncomingCall$CustomPhoneStateListener.onCallStateChanged(IncomingCall.java:105)
08-24 20:05:56.444: E/AndroidRuntime(28001): at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:393)
08-24 20:05:56.444: E/AndroidRuntime(28001): at android.os.Handler.dispatchMessage(Handler.java:99)
08-24 20:05:56.444: E/AndroidRuntime(28001): at android.os.Looper.loop(Looper.java:137)
08-24 20:05:56.444: E/AndroidRuntime(28001): at android.app.ActivityThread.main(ActivityThread.java:4898)
08-24 20:05:56.444: E/AndroidRuntime(28001): at java.lang.reflect.Method.invokeNative(Native Method)
08-24 20:05:56.444: E/AndroidRuntime(28001): at java.lang.reflect.Method.invoke(Method.java:511)
08-24 20:05:56.444: E/AndroidRuntime(28001): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
08-24 20:05:56.444: E/AndroidRuntime(28001): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
08-24 20:05:56.444: E/AndroidRuntime(28001): at dalvik.system.NativeStart.main(Native Method)
08-24 20:08:22.669: I/Process(28001): Sending signal. PID: 28001 SIG: 9
我也曾尝试过创建布局程序。 但没有运气
也无法弄清楚生成的布局的ID
答案 0 :(得分:3)
问题是,我们不能在广播接收器中使用对话框,而且必须在另一个可以用意图启动的活动中。
答案 1 :(得分:1)
当BroadcastReceiver接收Intent广播时,将调用此方法。在此期间,您可以使用BroadcastReceiver上的其他方法来查看/修改当前结果值。除非您明确要求使用registerReceiver(BroadcastReceiver,IntentFilter,String,android.os.Handler)在不同的线程上调度此方法,否则此方法始终在其进程的主线程内调用。当它在主线程上运行时,你永远不应该在其中执行长时间运行的操作(在考虑阻塞接收器和被杀死的候选者之前,系统允许超时10秒)。您无法在onReceive()的实现中启动弹出对话框。
Android文档明确说明了这一点。