2016-04-28 85 views
0

嗨在我的应用程序中,我想改变设备的调用者屏幕的外观和感觉,现在(我可能是错的)唯一的方法是调用弹出窗口在默认的android调用者屏幕上,因为你不能改变它。如何解决屏幕上的弹出窗口

我的弹出窗口出现时,用户按拨号,但然后它被默认的来电者屏幕取代,我的弹出窗口后面的默认调用者屏幕。

我该如何解决我的弹出窗口在屏幕上,以便它不被任何其他屏幕取代。

我DialerFragment.java:

package com.heroicjokester.android.haid; 

import android.Manifest; 
import android.content.Context; 
import android.content.Intent; 
import android.content.pm.PackageManager; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.Gravity; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.PopupWindow; 
import android.widget.RelativeLayout; 
import android.widget.Toast; 
import android.support.v4.content.ContextCompat; 

/** 
* Created by Ripcord on 01-Apr-16. 
*/ 
public class DialerFragment extends Fragment { 
private EditText mPhoneField; 
private Button mDialButton; 
private LinearLayout mLinearLayout; 
private LayoutInflater mLayoutInflater; 
private PopupWindow mPopupWindow; 

//Requesting Permissions on Runtime. 
final private int REQUEST_CODE_ASK_PERMISSIONS=0; 

private void InitiateCall(){ 
    int hasCallPermission = ContextCompat.checkSelfPermission(getActivity(),Manifest.permission.READ_PHONE_STATE); 
    if (hasCallPermission != PackageManager.PERMISSION_GRANTED){ 
     requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE}, 
       REQUEST_CODE_ASK_PERMISSIONS); 
     return; 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){ 
    switch (requestCode){ 
     case REQUEST_CODE_ASK_PERMISSIONS: 
      if (grantResults[0]==PackageManager.PERMISSION_GRANTED){ 
       //YAY! PERMISSION GRANTED 
       InitiateCall(); 
      }else{ 
       //GD! PERMISSION DENIED 
       Toast.makeText(getActivity(), R.string.permission_denied, Toast.LENGTH_SHORT).show(); 
      } 
      break; 
     default: 
      super.onRequestPermissionsResult(requestCode, permissions, grantResults); 

    } 
} 



@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

} 

@Override 
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){ 
    View v=inflater.inflate(R.layout.fragment_dialer,container,false); 

    mPhoneField=(EditText) v.findViewById(R.id.input_pno); 
    mDialButton=(Button) v.findViewById(R.id.dial_button); 
    mLinearLayout=(LinearLayout) v.findViewById(R.id.popup_linearlayout); 

    mDialButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      try { 
       if (mPhoneField != null && (mPhoneField.getText().length() == 10 || mPhoneField.getText().length() == 11)) { 
        startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mPhoneField.getText()))); 
       } else if (mPhoneField != null && mPhoneField.getText().length() == 0) { 
        Toast.makeText(getActivity(), R.string.no_number_toast, Toast.LENGTH_SHORT).show(); 
       } else if (mPhoneField != null && mPhoneField.getText().length() < 10) { 
        Toast.makeText(getActivity(), R.string.wrong_number_toast, Toast.LENGTH_SHORT).show(); 
       } 
      } catch (Exception e) { 
       Log.e("DialerAppActivity", "error: " + e.getMessage(), e);//Runtime error will be logged 
      } 

      mLayoutInflater = (LayoutInflater) getActivity().getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      ViewGroup container = (ViewGroup) mLayoutInflater.inflate(R.layout.dialer_popup, null); 

      mPopupWindow = new PopupWindow(container, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, false); 
      mPopupWindow.showAtLocation(mLinearLayout, Gravity.NO_GRAVITY, 0, 0); 

      container.setOnTouchListener(new View.OnTouchListener() { 
       @Override 
       public boolean onTouch(View view, MotionEvent motionEvent) { 
        Toast.makeText(getActivity(), R.string.hit_it, Toast.LENGTH_LONG).show(); 
        return true; 

       } 
      }); 

     } 
    }); 

    InitiateCall(); 

    return v; 


} 


} 
+0

你的问题是Intent.ACTION_CALL。这将去另一个应用程序的活动...这就是为什么你弹出不起作用 –

+0

那么有没有办法解决它? 有这样做的应用程序,他们是如何设法做到这一点的? – HeroicJokester

回答

0

发现这个代码,它使用广播Reciever 即时得到一个“无法解析变量为arg0和ARG1”错误任何想法如何清理?

// onReceive function of the Broadcast Receiver 
    public void onReceive(Context arg0, Intent arg1) { 
     String state = arg1.getStringExtra(TelephonyManager.EXTRA_STATE); 

     // Adds a view on top of the dialer app when it launches. 
     if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){ 
      wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); 
      params1 = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.MATCH_PARENT, 
        WindowManager.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 = 75; 
      params1.width = 512; 
      params1.x = 265; 
      params1.y = 400; 
      params1.format = PixelFormat.TRANSLUCENT; 

      mLinearLayout = new LinearLayout(getContext()); 
      mLinearLayout.setBackgroundColor(Color.BLACK); 
      mLinearLayout.setOrientation(LinearLayout.VERTICAL); 

      wm.addView(mLinearLayout, params1); 
     } 

     // To remove the view once the dialer app is closed. 
     if(arg1.getAction().equals("android.intent.action.PHONE_STATE")){ 
      state = arg1.getStringExtra(TelephonyManager.EXTRA_STATE); 
      if(state.equals(TelephonyManager.EXTRA_STATE_IDLE)){ 
       WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); 
       if(mLinearLayout!=null) 
       { 
        wm.removeView(mLinearLayout); 
        mLinearLayout = null; 
       } 
      } 
     } 
    }