美文网首页
Service中启动桌面悬浮窗

Service中启动桌面悬浮窗

作者: Alien的小窝 | 来源:发表于2016-10-19 10:48 被阅读868次

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

    package com.syntc.demo;
    
    import android.annotation.TargetApi;
    import android.content.Intent;
    import android.os.Build;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.example.lfk.myapplication.R;
    
    public class MainActivity extends AppCompatActivity {
    
       
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Intent intent = new Intent();
            intent.setClass(this, MyService.class);
            startService(intent);
            this.finish();
        }
    
    }
    
    
    package com.syntc.demo;
    
    import android.content.Context;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Rect;
    
    import android.util.AttributeSet;
    import android.view.View;
    
    /**
     * Created by lfk on 2016/8/31.
     */
    public class MyView extends View {
        private Paint mPaint;
        private Context mContext;
        private static final String mString = "Welcome to Mr Wei's blog";
    
        public MyView(Context context) {
            super(context);
    
        }
        public MyView(Context context,AttributeSet attr)
        {
            super(context,attr);
    
        }
        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
    
            mPaint = new Paint();
    
            //设置画笔颜色
            mPaint.setColor(Color.RED);
            //设置填充
            mPaint.setStyle(Paint.Style.FILL);
    
            //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
            canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);
    
            mPaint.setColor(Color.BLUE);
            //绘制文字
            canvas.drawText(mString, 10, 110, mPaint);
        }
    }
    
    
    
    
    package com.syntc.demo;
    
    import android.app.Service;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.graphics.PixelFormat;
    import android.os.IBinder;
    import android.view.View;
    import android.view.WindowManager;
    
    public class MyService extends Service implements View.OnClickListener {
        private WindowManager mWindowManager;
        private WindowManager.LayoutParams mLayoutParams;
        private MyView mMyView;
        private boolean flag = true;
        public MyService() {
            
        }
    
        @Override
        public void onCreate() {
            super.onCreate();
            mWindowManager = (WindowManager) getApplicationContext().getSystemService(
                    Context.WINDOW_SERVICE);
            mLayoutParams = new WindowManager.LayoutParams();
            mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;// 系统提示window
            mLayoutParams.format = PixelFormat.TRANSLUCENT;// 支持透明
            //mParams.format = PixelFormat.RGBA_8888;
            mLayoutParams.flags |= WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;// 焦点
            mLayoutParams.width = 490;//窗口的宽和高
            mLayoutParams.height = 160;
            mLayoutParams.x = 0;//窗口位置的偏移量
            mLayoutParams.y = 0;
            mMyView = new MyView(this);
            mMyView.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
    
        }
    
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            if (flag) {
                flag = false;
                mWindowManager.addView(mMyView, mLayoutParams);//添加窗口
            }
            return super.onStartCommand(intent, flags, startId);
        }
    
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            if (mMyView.getParent() != null)
                mWindowManager.removeView(mMyView);//移除窗口
            super.onDestroy();
        }
    
        @Override
        public IBinder onBind(Intent intent) {
            // TODO: Return the communication channel to the service.
            throw new UnsupportedOperationException("Not yet implemented");
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Service中启动桌面悬浮窗

          本文链接:https://www.haomeiwen.com/subject/hrcxuttx.html