您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關android6.0版本中怎么實現一個懸浮窗口,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
代碼如下:
public class MainActivity extends AppCompatActivity { private static final int ALERT_WINDOW_PERMISSION_CODE = 100; private Button start_float; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); start_float = (Button) findViewById(R.id.start_float); this.start_float.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if (Build.VERSION.SDK_INT > 22) { sdk23Permission(); } else { startService(new Intent(MainActivity.this, FloatService.class)); finish(); } } }); } /** * @description 安卓6.0下權限處理 * @author ldm * @time 2017/3/20 15:00 */ public void sdk23Permission() { if (!Settings.canDrawOverlays(this)) { Toast.makeText(MainActivity.this, "當前無權限使用懸浮窗,請授權!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())); startActivityForResult(intent, ALERT_WINDOW_PERMISSION_CODE); } else { startService(new Intent(MainActivity.this, FloatService.class)); finish(); } } /** * 用戶返回 */ protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == ALERT_WINDOW_PERMISSION_CODE) { if (!Settings.canDrawOverlays(this)) { Toast.makeText(MainActivity.this, "權限授予失敗,無法開啟懸浮窗", Toast.LENGTH_SHORT).show(); } else { startService(new Intent(MainActivity.this, FloatService.class)); finish(); } } } }
對應Service:
public class FloatService extends Service { @Nullable @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { FloatViewUtils.getInstance(this).addFloatView(); super.onCreate(); } }
簡單地FloatView:
public class FloatView extends View { public static final int WIDTH = 150; public static final int HEIGHT = 150; private Paint circlePaint; private Paint textPaint; private static final String text = "50%"; public FloatView(Context context) { this(context, null, 0); } public FloatView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public FloatView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initPaints(); } /** * @description 初始化相關畫筆Paint * @author ldm * @time 2017/3/20 */ private void initPaints() { circlePaint = new Paint(); circlePaint.setAntiAlias(true); circlePaint.setColor(Color.GRAY); textPaint = new Paint(); //設置抗鋸齒 textPaint.setAntiAlias(true); //設置字體大小 textPaint.setTextSize(30); //設置顏色 textPaint.setColor(Color.WHITE); //設置(仿)粗體 textPaint.setFakeBoldText(true); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(WIDTH, HEIGHT); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(WIDTH / 2, HEIGHT / 2, WIDTH / 2, circlePaint); float textWidth = textPaint.measureText(text); float x = WIDTH / 2 - textWidth / 2; Paint.FontMetrics fms = textPaint.getFontMetrics(); float dy = -(fms.descent + fms.ascent) / 2; float y = HEIGHT / 2 + dy; canvas.drawText(text, x, y, textPaint); } }
以及FloatView管理工具類:
public class FloatViewUtils { private static FloatViewUtils instance; private Context mContext; private WindowManager manager; private FloatView floatView; private FloatViewUtils(Context mContext) { this.mContext = mContext; manager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE); floatView = new FloatView(mContext); } public static FloatViewUtils getInstance(Context mContext) { if (null == instance) { synchronized (FloatViewUtils.class) { if (null == instance) { instance = new FloatViewUtils(mContext); } } } return instance; } public void addFloatView() { WindowManager.LayoutParams lp = new WindowManager.LayoutParams(); //懸浮窗口大小 lp.width = floatView.WIDTH; lp.height = floatView.HEIGHT; // 調整懸浮窗口位置 lp.gravity = Gravity.LEFT | Gravity.CENTER; // 以屏幕左上角為原點,設置x、y初始值 // lp.x = 0; // lp.y = 0; //設置懸浮窗口類型 lp.type = WindowManager.LayoutParams.TYPE_PHONE; //設置懸浮窗口不接受焦點及觸摸事件 lp.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE; //設置圖片格式,效果為背景透明 lp.format = PixelFormat.RGBA_8888; manager.addView(floatView, lp); } }
最后不要忘記在AndroidManifest.xml中添加權限(當然還有注冊Service):
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
關于android6.0版本中怎么實現一個懸浮窗口就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。