如何在来电时显示叠加(如truecaller应用)

时间:2014-09-15 11:00:13

标签: android

希望在来电时显示叠加层(如truecaller应用)。我检查了  Pop up window over Android native incoming call screen like true caller Android app此链接并尝试执行但它在单独的页面中显示值..

更新:   我使用WindowManager解决了这个问题,但现在问题是我无法删除我试过的视图如下,请帮我解决这个问题

  public void onReceive(Context context, Intent intent) {
    Log.i("", "Intent " + intent.toString());
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) { 
        String mVal;
        try {
            mVal = new GetCallContacts(context).execute().get();

            wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            WindowManager.LayoutParams params;
            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_TOUCHABLE ,
                    PixelFormat.TRANSPARENT); 

            params.gravity = Gravity.TOP;
            LayoutInflater mInflater = LayoutInflater.from(context);
            mView = mInflater.inflate(R.layout.activity_incoming_call, null);
            TextView tv = (TextView) mView.findViewById(R.id.textView1);
            tv.setText("BJP Contacts: "+mVal);   

                wm.addView(mView, params);
        } catch (Exception e) {
            Log.e(state, e.toString())  ;
        }  
   } else if (intent.getAction().equals(BOOT_COMPLETED)) {
        context.startService(intent);

   } else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)
          || state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

      try {


        ((WindowManager)  context.getSystemService(Context.WINDOW_SERVICE)).removeView(mView);//wm.removeView(mView);// it does not remove view
      } catch (Exception e) {
      }
  }  

}

1 个答案:

答案 0 :(得分:0)

试试这个:

wm = (WindowManager) mcontext.getSystemService(Context.WINDOW_SERVICE);
            params1 = 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);

            params1.height = 280;
            params1.width = LayoutParams.MATCH_PARENT;
            params1.gravity = Gravity.TOP;
            params1.format = PixelFormat.TRANSLUCENT;

            ly1 = new LinearLayout(mcontext.getApplicationContext());
            ly1.setOrientation(LinearLayout.HORIZONTAL);
            ly1.setBackgroundColor(Color.WHITE);
            View hiddenInfo = inflater.inflate(R.layout.call_screen_overlay, ly1,
false);
            tv1 = (TextView) hiddenInfo.findViewById(R.id.number);
             tv1.setText(incomingNumber);
             tv2 = (TextView) hiddenInfo.findViewById(R.id.ctype);
             tv2.setText("Incoming...");
            tv2.setText("Incoming...");
            ly1.addView(hiddenInfo);
            wm.addView(ly1, params1);

在EXTRA_STATE_IDLE或EXTRA_STATE_OFFHOOK中使用以下条件删除视图:

if(ly1!=null)
            {
                Log.i("STage", "Remove View");
                wm.removeView(ly1);
                ly1 = null;
            }

希望它有所帮助。