Android桌面懸浮窗

//注意6.0權(quán)限申請

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;
//創(chuàng)建浮動窗口設(shè)置布局參數(shù)的對象
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);
    //設(shè)置window type
    wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
    //設(shè)置圖片格式,效果為背景透明
    wmParams.format = PixelFormat.RGBA_8888;
    //設(shè)置浮動窗口不可聚焦(實現(xiàn)操作除浮動窗口外的其他可見窗口的操作)
    wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    //調(diào)整懸浮窗顯示的停靠位置為左側(cè)置頂
    wmParams.gravity = Gravity.LEFT | Gravity.TOP;
    // 以屏幕左上角為原點,設(shè)置x、y初始值,相對于gravity
    wmParams.x = 0;
    wmParams.y = 0;

    //設(shè)置懸浮窗口長寬數(shù)據(jù)
    wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
    wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

     /*// 設(shè)置懸浮窗口長寬數(shù)據(jù)
    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);

    //設(shè)置監(jiān)聽浮動窗口的觸摸移動
 /*   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為狀態(tài)欄的高度
            wmParams.y = (int) event.getRawY() - mFloatView.getMeasuredHeight();
            //刷新
            mWindowManager.updateViewLayout(mFloatView, wmParams);
            return false;  //此處必須返回false,否則OnClickListener獲取不到監(jiān)聽
        }
    });*/

    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(), "發(fā)送中", 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(), "發(fā)送成功", Toast.LENGTH_SHORT).show();
                }
            });
}

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

}

技術(shù)交流群:633600411

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容