Android关闭TYPE_SYSTEM_ALERT,返回main_activity视图

时间:2014-02-26 15:16:18

标签: java android android-activity views ontouchlistener

我有一个系统覆盖图打开的代码,但是如何关闭它并发送回main_activity视图。

我尝试使用wm.removeView(myView);myView.setVisibility(View.GONE);,但它仍然可见,我是否必须加载主要活动的视图?

感谢您的帮助!


这里是主要的活动类:

public class MainShakeDetector_2 extends Activity {

    Context myContext;
    InvisibleView myInvisibleView;
    View overlayView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_shake_detector_2);

        myInvisibleView = new InvisibleView(this);
    }

    // ------- --------- --------- --------- ----------
    // ------- method to open the system layout view
    // ------- --------- --------- --------- ----------
     public void startApp(View activity_main_shake_detector_2){

            myContext = this.getApplicationContext();
            myInvisibleView.onCreate(myContext);

            overlayView = new View(myContext);
            setContentView(R.layout.overlay);
            overlayView= (View) findViewById(R.id.overlayViewId);

    }   

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_shake_detector_2, menu);
        return true;
    }

}

这里是系统上的

public class InvisibleView extends Service {

    View myView;
    Activity parent;

    @Override
    public IBinder onBind(Intent intent) {  return null;  }
    public InvisibleView (Activity parent) {    this.parent = parent;   }

    public void onCreate(final Context myContext) { 
       super.onCreate();
       LayoutInflater inflater = (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
       myView = new View(myContext);
       myView = inflater.inflate(R.layout.overlay, null);

       WindowManager.LayoutParams params = new WindowManager.LayoutParams(
               WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
               WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                       | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
               PixelFormat.TRANSLUCENT);


        final WindowManager wm = (WindowManager) myContext.getSystemService(WINDOW_SERVICE);
        wm.addView(myView, params);



        // ------------ ------------ ------------ ------------ ------------
        // here the button which should close this system over lay
        // ------------ ------------ ------------ ------------ ------------

        Button b = (Button) myView.findViewById(R.id.overLayButton);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("click","click von system overley");
                wm.removeView(myView);

            }
        });

    }



  @Override
  public void onDestroy() {
      super.onDestroy();
        if(myView != null)
      {
          ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(myView);
          myView = null;
     }
  }
}

0 个答案:

没有答案