我可以使用按钮点击事件从服务中打开新的活动吗?

时间:2014-08-09 05:06:20

标签: java android service

有可能我不知道。当我点击按钮然后重定向特定活动的页面。我正在主屏幕上创建泡泡按钮,例如facebook messenger。

public class AbhanService extends Service {

private WindowManager windowManager;
private ImageView globalView;
private boolean inDragMode;
private int screenWidth, screenHight;
private int initX;
private int initY;
private int initViewX, initViewY;
private int newX, newY;
private Context context;

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {
    super.onCreate();
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    DisplayMetrics displaymetrics = new DisplayMetrics();
    windowManager.getDefaultDisplay().getMetrics(displaymetrics);
    screenWidth = displaymetrics.widthPixels;
    screenHight = displaymetrics.heightPixels;
    inDragMode = false;
    globalView = new ImageView(this);
    globalView.setImageResource(R.drawable.ic_launcher);
    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 = 0;

    globalView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "Click...", 500).show();
            // Intent intent = new Intent(context, AbhanActivity.class);
            // intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            // context.startActivity(intent);
        }
    });

    globalView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                inDragMode = true;
                initX = params.x;
                initY = params.y;
                initViewX = Math.abs((int) event.getRawX()
                        - ((ImageView) view).getLeft());
                initViewY = Math.abs((int) event.getRawY()
                        - ((ImageView) view).getTop());
            case MotionEvent.ACTION_UP:
                inDragMode = false;

            case MotionEvent.ACTION_MOVE:
                if (!inDragMode) {
                    newX = initX + (int) event.getRawX() - initViewX;
                    newY = initY + (int) event.getRawY() - initViewY;
                    if ((newX <= 0 || newX >= screenWidth)
                            || (newY <= 0 || newY >= screenHight)) {
                        inDragMode = false;
                    } else {
                        params.x = newX;
                        params.y = newY;
                        updateViews(globalView, params);
                    }
                }
            }
            return false;
        }
    });
    windowManager.addView(globalView, params);

}

private void updateViews(final ImageView headImage,
        final WindowManager.LayoutParams params) {
    windowManager.updateViewLayout(headImage, params);
}

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

}

按钮显示在主屏幕上,但不重定向活动。我不知道是什么问题。请给我任何一种解决方法。

2 个答案:

答案 0 :(得分:1)

尝试使用此按钮进行内部按钮单击。

int timeout = 4000; Timer timer = new Timer(); timer.schedule(new TimerTask() {

@Override
public void run() {
    finish();
    Intent homepage = new Intent(Activity1.this, Activity2.class);
    startActivity(homepage);
}

},超时);`

答案 1 :(得分:1)

Intent.FLAG_ACTIVITY_NEW_TASK添加到您用于启动活动的Intent,例如

Intent intent = new Intent(context, AbhanActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);