Android桌面悬浮窗

2017-05-15  本文已影响0人  ana生

//注意6.0权限申请

private void requestCameraPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW}, REQUEST_PERMISSION_CAMERA_CODE);
    } else {
        Log.e("TAG", "----");
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_PERMISSION_CODE) {
        int grantResult = grantResults[0];
        boolean granted = grantResult == PackageManager.PERMISSION_GRANTED;
        Log.e("TAG", "onRequestPermissionsResult granted=" + granted);
    }
}

简单一句话
startService(new Intent(MainActivity.this, FxService.class));

public class FxService extends Service {


WindowManager.LayoutParams wmParams;
//创建浮动窗口设置布局参数的对象
WindowManager mWindowManager;

Button mFloatView;

private static final String TAG = "FxService";

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    Log.i(TAG, "oncreat");
    createFloatView();
}

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

private void createFloatView() {
    wmParams = new WindowManager.LayoutParams();
    //获取的是WindowManagerImpl.CompatModeWrapper
    mWindowManager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
    Log.i(TAG, "mWindowManager--->" + mWindowManager);
    //设置window type
    wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
    //设置图片格式,效果为背景透明
    wmParams.format = PixelFormat.RGBA_8888;
    //设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
    wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    //调整悬浮窗显示的停靠位置为左侧置顶
    wmParams.gravity = Gravity.LEFT | Gravity.TOP;
    // 以屏幕左上角为原点,设置x、y初始值,相对于gravity
    wmParams.x = 0;
    wmParams.y = 0;

    //设置悬浮窗口长宽数据
    wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

     /*// 设置悬浮窗口长宽数据
    wmParams.width = 200;
    wmParams.height = 80;*/

    mFloatView = new Button(getApplicationContext());
    mFloatView.setText(R.string.shot);
    mFloatView.setBackground(getResources().getDrawable(R.drawable.btn_selector));
    mFloatView.setWidth(30);
    mFloatView.setHeight(15);
    //添加mFloatLayout
    mWindowManager.addView(mFloatView, wmParams);

    //设置监听浮动窗口的触摸移动
 /*   mFloatView.setOnTouchListener(new View.OnTouchListener()
    {

        @Override
        public boolean onTouch(View v, MotionEvent event)
        {
            // TODO Auto-generated method stub

            //getRawX是触摸位置相对于屏幕的坐标,getX是相对于按钮的坐标
            wmParams.x = (int) event.getRawX() - mFloatView.getMeasuredWidth();
            //减25为状态栏的高度
            wmParams.y = (int) event.getRawY() - mFloatView.getMeasuredHeight();
            //刷新
            mWindowManager.updateViewLayout(mFloatView, wmParams);
            return false;  //此处必须返回false,否则OnClickListener获取不到监听
        }
    });*/

    mFloatView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String filePath = MainActivity.getRecentlyPhotoPath(getApplicationContext());
            requestForUploadPic(filePath);
        }
    });
}

private void requestForUploadPic(String filePath) {
    Toast.makeText(getApplicationContext(), "发送中", Toast.LENGTH_SHORT).show();
    File picFile = new File(filePath);
    OkHttpUtils.post()
            .url("http://csf.f3322.net:9999/uploadservlet")
            .addFile("uploadFile", filePath, picFile)
            .build()
            .execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                }

                @Override
                public void onResponse(String response, int id) {
                    Log.e("TAG", "onResonse: " + response);
                    Toast.makeText(getApplicationContext(), "发送成功", Toast.LENGTH_SHORT).show();
                }
            });
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if (mFloatView != null) {
        //移除悬浮窗口
        mWindowManager.removeView(mFloatView);
    }
}

}

技术交流群:633600411

上一篇下一篇

猜你喜欢

热点阅读