无法隐藏TYPE_SYSTEM_ALERT视图

时间:2015-05-05 17:58:26

标签: android

有一个Service可以让BroadcastReceiver听取来电。根据数据库中存在的数量,显示RelativeLayout。但是当挂断呼叫时,必须从屏幕上删除RelativeLayout。尝试删除之前添加的View时,下面的代码会引发异常:

@Override
public void onReceive(Context context, Intent intent) {
    View view = View.inflate(context, R.layout.toast_custom_incoming_call,
            null);
    TextView textView = (TextView) view.findViewById(R.id.tx);
    final WindowManager manager = (WindowManager) context
            .getApplicationContext().getSystemService(
                    Context.WINDOW_SERVICE);

    if (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
            TelephonyManager.EXTRA_STATE_RINGING)) {
        Log.d(TAG, "BROADCAST RECEIVER WORKING");

        String incomingNumber = intent
                .getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
        Log.d(TAG, "Call from: " + incomingNumber.trim());
        SQLiteDatabase database;
        try {
            database = new DeliveryOrder().db().getReadableDatabase();
        } catch (SQLiteException e) {
            // database was locked
            e.printStackTrace();
            return;
        }
        Cursor cursor = database.query("res_partner", new String[] {
                "street", "city", "zip", "website" }, "phone = ?",// or
                                                                    // mobile?
                new String[] { incomingNumber }, null, null, null);

        // assuming that the number is UNIQUE for each partner only 1 result
        // will be returned, if the phone number doesn't belong to any of
        // the records (eg. query returned no records) do not try to get
        // anything from the cursor
        String[] row = new String[4];

        if (cursor.moveToFirst()) {
            Log.d(TAG, "CURSOR MOVED TO FIRST");

            for (int i = 0; i < cursor.getColumnCount(); i++)
                row[i] = cursor.getString(i);

            for (int i = 0; i < row.length; i++) {
                if (row[i].equals("false") || row[i] == null) {
                    row[i] = "";
                }
            }
            textView.setText(row[0] + row[1] + row[2]);
        }

        Notification.Builder builder = new Notification.Builder(context);
        builder.setSmallIcon(R.drawable.ic_launcher_mediapp);
        builder.setContentTitle(row[0] + row[1] + row[2]);
        builder.setContentText(row[0] + row[1] + row[2]);
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(NOTIFICATION_ID, builder.build());

        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        manager.addView(view, layoutParams);

        Log.d(TAG, "ROW: " + row[0] + row[1] + row[2]);
        cursor.close();
        database.close();

    } else {
        // call hanged, remove the alert
         manager.removeView(view);

    }



05-05 20:57:51.992: E/AndroidRuntime(4768): FATAL EXCEPTION: main
05-05 20:57:51.992: E/AndroidRuntime(4768): java.lang.RuntimeException: Error receiving broadcast Intent { act=android.intent.action.PHONE_STATE flg=0x10 (has extras) } in com.MediAPP.receivers.IncomingCallReceiver@4111e6a0
05-05 20:57:51.992: E/AndroidRuntime(4768):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:765)
05-05 20:57:51.992: E/AndroidRuntime(4768):     at android.os.Handler.handleCallback(Handler.java:615)
05-05 20:57:51.992: E/AndroidRuntime(4768):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-05 20:57:51.992: E/AndroidRuntime(4768):     at android.os.Looper.loop(Looper.java:137)
05-05 20:57:51.992: E/AndroidRuntime(4768):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-05 20:57:51.992: E/AndroidRuntime(4768):     at java.lang.reflect.Method.invokeNative(Native Method)
05-05 20:57:51.992: E/AndroidRuntime(4768):     at java.lang.reflect.Method.invoke(Method.java:511)
05-05 20:57:51.992: E/AndroidRuntime(4768):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-05 20:57:51.992: E/AndroidRuntime(4768):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-05 20:57:51.992: E/AndroidRuntime(4768):     at dalvik.system.NativeStart.main(Native Method)
05-05 20:57:51.992: E/AndroidRuntime(4768): Caused by: java.lang.IllegalArgumentException: View not attached to window manager
05-05 20:57:51.992: E/AndroidRuntime(4768):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:653)
05-05 20:57:51.992: E/AndroidRuntime(4768):     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:349)
05-05 20:57:51.992: E/AndroidRuntime(4768):     at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:160)
05-05 20:57:51.992: E/AndroidRuntime(4768):     at com.MediAPP.receivers.IncomingCallReceiver.onReceive(IncomingCallReceiver.java:99)
05-05 20:57:51.992: E/AndroidRuntime(4768):     at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:755)
05-05 20:57:51.992: E/AndroidRuntime(4768):     ... 9 more

1 个答案:

答案 0 :(得分:2)

您在View每个onReceive充气View。当然,当您尝试删除{{1}}时,尚未添加{{1}}。