0

我想在屏幕左侧获得一个应用程序菜单栏,而不会干扰已经运行的任务,例如动态壁纸或视频。如何实现?

4

3 回答 3

0

请使用此服务等级。通过从您的活动中启动此服务,您将在手机屏幕上获得一张图片。通过调整,您可以设置手机中的任何位置。您可以随时通过停止服务来删除它。

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date; 
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.os.Environment;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewDebug;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class HUD extends Service {
    HUDView mView;

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

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

        mView = new HUDView(this);
        mView.setId(R.id.button);
        WindowManager.LayoutParams paramsOrg = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,

                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);




        WindowManager.LayoutParams params = new WindowManager.LayoutParams(100, 100, 2007, 8, -3);
        Button bb=new Button(this);
        bb.setText("Button");
        bb.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                System.out.println("Clicked----><<<<<<<");
            }
        });

        bb.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                System.out.println("Touched =----- > ");
                return false;
            }
        });

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




        wm.addView(bb, params);



    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        //Toast.makeText(getBaseContext(),"onDestroy", Toast.LENGTH_LONG).show();
        if(mView != null)
        {
            ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(mView);
            mView = null;
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        // Checks the orientation of the screen
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
            restartService();
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
            //Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
            restartService();
        }

    }

    private void restartService() {
        // TODO Auto-generated method stub
        stopService(new Intent(getBaseContext(), HUD.class));
        startService(new Intent(getBaseContext(), HUD.class));
    }
}

class HUDView extends ViewGroup {
    private Paint mLoadPaint;
    private Float XMAX=280f;
    private Float YMAX=26f;
    private int XPOS=0;     

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


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


        context.bindService(intents, aslServiceConnection, Context.BIND_AUTO_CREATE);


        WindowManager wind = (WindowManager)context.getSystemService(Service.WINDOW_SERVICE);
        XPOS=wind.getDefaultDisplay().getWidth();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //canvas.drawText("Hello World", 5, 15, mLoadPaint);
        //canvas.drawText("Hello World", 100, 100, mLoadPaint);
        Bitmap tileImage = BitmapFactory.decodeResource(getResources(), R.drawable.image);

        canvas.drawBitmap(tileImage, XPOS-35, 0, null);
        XMAX=Float.parseFloat(""+(XPOS-35));

    }

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

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        Float x=event.getX();
        Float y=event.getY();
        System.out.println("Mview has been touched  --->  "+x +" x "+y);
        if(x > 0 && y > 0)
        {
            if(x > XMAX && y < YMAX){

                TakeMyScreen(getContext());


            }

        }



        return true;
    }




    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        return super.onKeyDown(keyCode, event);
    }


}
于 2013-01-09T07:24:27.013 回答
0

必须在项目清单中使用权限,否则窗口未附加

android:name="android.permission.SYSTEM_ALERT_WINDOW"
于 2013-10-23T07:32:46.783 回答
0

您可以使用来自 github 的 NewPopupMenu 库

它将在任何侦听器的屏幕上的任何位置创建即时弹出菜单。

您只需下载库并解压缩,将源文件复制到项目中,并在此行修改代码mPopupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0); 并将菜单放在您想要的任何位置。

于 2013-01-09T06:58:25.927 回答