Admob锁定调用屏幕android

时间:2015-02-28 13:42:12

标签: android broadcastreceiver admob call phone-state-listener

我想在电话屏幕上显示广告。但有些事情是错的。我试图只显示窗口管理员的孩子的1个视图。它显示广告很好,但我不能拒绝或接受电话。我也尝试非任务和线程或其他。请帮我解决这个问题。谢谢:)

public class StateListener extends PhoneStateListener {

AdvertisementService advertisementService = new AdvertisementService();
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private Intent intent;
private static AdView mView =null;
private WindowManager manager;
private static final String AdID = "<TEST_DEVICE_ID>";
private AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdID).build();
Context context;
public StateListener(Context context) {
    this.context = context;
    LoadAdvertisementTask task = new LoadAdvertisementTask();
    task.execute();
}
private class LoadAdvertisementTask extends AsyncTask<Void,Void,Void>{

    private WindowManager.LayoutParams params;
    @Override
    protected void onPreExecute() {
        if(mView==null){
            mView = new AdView(context);
            mView.setAdUnitId(context.getResources().getString(R.string.banner_ad_unit_id));
            mView.setAdSize(AdSize.BANNER);

        }
        mView.loadAd(adRequest);
        manager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
                PixelFormat.TRANSLUCENT);
        params.gravity = Gravity.TOP | Gravity.LEFT;
        params.x = 0;
        Display display = manager.getDefaultDisplay();
        Point pnt = new Point();
        display.getSize(pnt);
        params.y = pnt.y/2;
    }

    @Override
    protected Void doInBackground(Void... voids) {

        try{

        }catch(Exception ex){
            ex.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        mView.setVisibility(View.INVISIBLE);
        manager.addView(mView, params);
    }
}


@Override
public void onCallStateChanged(int state, String incomingNumber) {
    switch (state){

        case TelephonyManager.CALL_STATE_IDLE:
            Toast.makeText(context,"IDLE",Toast.LENGTH_SHORT).show();
            if(mView!=null)
                mView.setVisibility(View.INVISIBLE);
           // manager.removeView(mView);
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            Toast.makeText(context,"OFFHOOK",Toast.LENGTH_SHORT).show();
            if(mView!=null)
                mView.setVisibility(View.INVISIBLE);
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            if(mView!=null)
            mView.setVisibility(View.INVISIBLE);
            //telefon caliyo

            Toast.makeText(context,"RINGRING",Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
    }
  }
}

0 个答案:

没有答案