4

当此按钮点击调用的后台服务时,我有一个带有就绪按钮的 UI。我需要在主屏幕上设置位图图像,就像屏幕上的划痕一样。有没有办法在不显示活动布局的情况下做到这一点。或者,即使我们使用布局在屏幕上显示该图像,那么我也需要 background (home、buttons 和所有其他当前打开的东西) active.

我附上了上面的图片作为参考,你可以看到我想要做什么。请帮我。您的关注将不胜感激。

4

1 回答 1

1

对你来说这是迟到的答案,但也许它可以拯救别人的一天...... :)

Permission : android.permission.SYSTEM_ALERT_WINDOW

 class ScreenOverlayService extends Service {
HUDView mView;

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

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Log.i("onStartCommand", "called....");      

    //      return android.app.Service.START_STICKY;    // don't kill service...
    return START_NOT_STICKY;
}

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

    mView = new HUDView(this);

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.RIGHT | Gravity.TOP;
    params.setTitle("Load Average");
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.addView(mView, params);

}

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

    if(mView != null){
        ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
        mView = null;
    }
}

class HUDView extends ViewGroup {
    private Paint mLoadPaint;
    Context context;

    public HUDView(Context context_)
    {
        super(context_);
        context = context_;

        mLoadPaint = new Paint();
        mLoadPaint.setAntiAlias(true);
        mLoadPaint.setTextSize(10);
        mLoadPaint.setARGB(255, 255, 0, 0);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.drawText("Hello World", 5, 15, mLoadPaint);

        // Read the image
        Bitmap markerImage = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);

        //  Draw it, centered around the given coordinates
        canvas.drawBitmap(markerImage,
                screenPoint.x - markerImage.getWidth() / 2,
                screenPoint.y - markerImage.getHeight() / 2, null);

    }

    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return super.onTouchEvent(event);
    }
}

}

于 2013-09-24T19:53:06.503 回答