Chathead在打开新活动时保持前景

时间:2014-04-14 18:36:11

标签: android

chatHead的正常功能是它出现在所有打开的活动中。当我打开一个新活动时,我希望chatHead能够保持前景。例如,当我打开另一个应用程序,如联系人或消息chatHead不应出现。这可能吗?请告知代码,谢谢

飞翔课程:

public class Fly extends Service {


private WindowManager windowManager;
private ImageView chatHead;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

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

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    chatHead = new ImageView(this);

    chatHead.setImageResource(R.drawable.floating);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,

            WindowManager.LayoutParams.TYPE_PHONE,

            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,

            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT;
    params.x = 0;
    params.y = 100;


    windowManager.addView(chatHead, params);



    try {
        chatHead.setOnTouchListener(new View.OnTouchListener() {
            private WindowManager.LayoutParams paramsF = params;
            private int initialX;
            private int initialY;
            private float initialTouchX;
            private float initialTouchY;

            @Override public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                    // Get current time in nano seconds.

                    initialX = paramsF.x;
                    initialY = paramsF.y;
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
                    paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
                    windowManager.updateViewLayout(chatHead, paramsF);
                    break;
                }
                return false;
            }
        });
    } catch (Exception e) {
        // TODO: handle exception
    }

}



@Override
public void onDestroy() {
    super.onDestroy();
    if (chatHead != null) windowManager.removeView(chatHead);
    //windowManager.removeView(chatHead);


}

}

MainActivity:

public class MainActivity extends Activity {

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

    Bundle bundle = getIntent().getExtras();

    if(bundle != null && bundle.getString("LAUNCH").equals("YES")) {
        startService(new Intent(MainActivity.this, Fly.class));
    }

    Button launch = (Button)findViewById(R.id.button1);
    launch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startService(new Intent(MainActivity.this, Fly.class));
        }
    });

    Button stop = (Button)findViewById(R.id.button2);
    stop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            stopService(new Intent(MainActivity.this, Fly.class));
        }
    });

}

@Override
protected void onResume() {
    Bundle bundle = getIntent().getExtras();

    if(bundle != null && bundle.getString("LAUNCH").equals("YES")) {
        startService(new Intent(MainActivity.this, Fly.class));
    }
    super.onResume();
}

protected void onPause(){

    if (chatHead != null) windowManager.removeView(chatHead);

    super.onPause();

}

}

1 个答案:

答案 0 :(得分:0)

您的聊天标记已添加到WindowManager,该onPause显示在应用程序中的所有其他位置上方,包括新启动的活动。如果您不希望它显示在某个Activity之外,则应通过向Service发送消息,手动删除Activity Service函数中的chatHead。

当代码收到来自活动的消息时,需要在if (chatHead != null) { windowManager.removeView(chatHead); } 中运行。

chatHead

您还可以保存Service视图的参数,并通过向Activity的{​​{1}}中的onResume发送不同的消息来重新添加该参数。

请参阅Android Service documentation了解如何设置消息。


另一种选择是根本不使用服务,而是将所有代码从中移动到您的活动中。然后,您可以直接控制是否从Activity的{​​{1}}和onPause功能中显示chatHead。请参阅this helpful tutorial on Pausing and Resuming an Activity