Android长久浮窗 service

/*
--TYPE_SYSTEM_ALERT : 系统警告提示。
--TYPE_SYSTEM_ERROR : 系统错误提示。
--TYPE_SYSTEM_OVERLAY : 页面顶层提示。
--TYPE_SYSTEM_DIALOG : 系统对话框。
--TYPE_STATUS_BAR : 状态栏
--TYPE_TOAST : 短暂通知Toast
*/
package com.example.suspendview;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.TextView;
public class TopFloatService extends Service {
	private View view;
	private WindowManager windowManager;
	
	private WindowManager.LayoutParams layoutParams;
	private TextView text;
	private Context context;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		creatView();
		
	}
	private void creatView() {
		// TODO Auto-generated method stub
		view = LayoutInflater.from(this).inflate(R.layout.main, null);
		text = (TextView) view.findViewById(R.id.flowspeed);
		text.setText("工厂模式");
		windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
		
		/*
		 * 	TYPE_SYSTEM_OVERLAY:窗口显示的时候焦点在后面的Activity上,仍旧可以操作后面的Activity
		 * LayoutParams.TYPE_SYSTEM_ERROR:保证该悬浮窗所有View的最上层 ,不可点后面的activity
		 * LayoutParams.FLAG_NOT_FOCUSABLE:该浮动窗不会获得焦点
		 * PixelFormat.TRANSPARENT:悬浮窗透明
		 */
		layoutParams = new WindowManager.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
		LayoutParams.TYPE_SYSTEM_OVERLAY, LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSPARENT);
		layoutParams.gravity= Gravity.TOP; 
		layoutParams.x=160;
	//	layoutParams.y=160;
		 windowManager.addView(view, layoutParams);
		 Log.e("st", "creat");
	}
	
	public void closeViwe(){
		windowManager.removeView(view);
	}
		
	private void OnInterceptTouchEvent() {
		// TODO Auto-generated method stub
	}
}

package com.example.suspendview;
import java.io.File;
import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
/**
 * 
* @ClassName: MainActivity
* @Description: TODO(这里用一句话描述这个类的作用)
* @date 2017-6-15
* 实现悬浮窗功能
 */
public class MainActivity extends Activity {
	private Button bt;
	private TextView tv;
	private TopFloatService tp;
	WindowManager windowmage;
	private Context context;
	Intent intent;
	View view;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         tv =(TextView) findViewById(R.id.tv);
         tp = new TopFloatService();
    
         
        boolean tt=isExist();
        tv.setText(tt+"");
         intent = new Intent(MainActivity.this, TopFloatService.class);  
        startService(intent);  
       // finish();  
       
        
    }
 
    
    public void bt(View view){
    	
    	 intent = new Intent(MainActivity.this, TopFloatService.class);  
    	 stopService(intent);
       	tp.closeViwe();
        Log.e("st", "close");
    }
}

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



  <service android:name="com.example.suspendview.TopFloatService">   </service>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView  
        android:id="@+id/flowspeed"  
        android:textSize="60sp"
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="工厂设置"
        android:textColor="#ff00ff" />  
  
  
</LinearLayout>

设置view到任意位置实现

    public void addWindowView(View statusBarView) {
        mLp = new WindowManager.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                0,
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                        | WindowManager.LayoutParams.FLAG_SPLIT_TOUCH
                        | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                        | WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS,
                PixelFormat.TRANSPARENT);
        mLp.token = new Binder();
       // mLp.gravity = Gravity.TOP;
        mLp.y = -45;
        mLp.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
     //   mLp.setTitle("StatusBar");
        mLp.packageName = mContext.getPackageName();
        mStatusBarView = statusBarView;
        //   mStatusBarView.setBackgroundColor(SystemUIApplication.getInstance().getColor(android.R.color.background_dark));
        mWindowManager.addView(mStatusBarView, mLp);
        mLpChanged = new WindowManager.LayoutParams();
        mLpChanged.copyFrom(mLp);
    }
```
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android 实现漂浮窗提示框的方法如下: 1. 在 AndroidManifest.xml 文件中添加 SYSTEM_ALERT_WINDOW 权限: ```xml <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> ``` 2. 创建一个 Service 类,用于显示浮窗提示框: ```java public class FloatingWindowService extends Service { private WindowManager mWindowManager; private View mFloatingView; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); // 创建一个 WindowManager 对象 mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); // 创建一个 View 对象,作为漂浮窗提示框的界面 mFloatingView = LayoutInflater.from(this).inflate(R.layout.floating_window, null); // 设置漂浮窗提示框的位置和大小 WindowManager.LayoutParams params = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); params.gravity = Gravity.TOP | Gravity.START; params.x = 0; params.y = 100; // 将漂浮窗提示框添加到 WindowManager 中 mWindowManager.addView(mFloatingView, params); } @Override public void onDestroy() { super.onDestroy(); // 在 Service 销毁时,移除漂浮窗提示框 if (mFloatingView != null) { mWindowManager.removeView(mFloatingView); } } } ``` 3. 在布局文件中定义漂浮窗提示框的界面,例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_notification" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一条漂浮窗提示框" android:textColor="#FFFFFF" /> </LinearLayout> ``` 4. 在 Activity 中启动 FloatingWindowService: ```java Intent intent = new Intent(this, FloatingWindowService.class); startService(intent); ``` 这样就可以在 Android 应用中实现漂浮窗提示框了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FW_G8Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值