非原创! 在网上看到别的大神发的,自己总结了一下,发现挺有意思的,发此博客纯属个人收集!忘大神见谅!
利用Service设置手机桌面的悬浮窗口,退出应用后,开启在手机桌面的一个小窗口,挺有意思的。看图:
同样可以完成点击事件啊,数据交互啊!
好了,上代码:
首先我写的demo只有一个button,在其监听事件中启动的服务,
最重要的部分都在服务中呢!
package com.example.floatwindow;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity{
private Button windowBtn;
public static Intent floatIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
windowBtn = (Button) findViewById(R.id.start_window);
windowBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
floatIntent = new Intent(MainActivity.this, FloatWindowService.class);
startService(floatIntent);
}
});
}
}
Activity中的Intent定义为静态的了,纯属个人懒,为了停止此服务而设置的,如果有大神拥有更好的办法,请评论告知,不胜感谢!!
之后是服务
FloatWindowService
package com.example.floatwindow;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class FloatWindowService extends Service{
// 定义桌面窗口布局
private LinearLayout floatLayout;
// 是WindowManager的LayoutParams
private LayoutParams params;
// 桌面窗口布局参数设置类
private WindowManager manager;
@Override
public void onCreate() {
super.onCreate();
// 调用方法,生成桌面窗口
createFloatView();
}
/**
* 生成桌面窗口方法
*/
private void createFloatView() {
// 初始化桌面窗口参数和管理类对象
params = new WindowManager.LayoutParams();
manager = (WindowManager) getApplication().getSystemService(getApplication().WINDOW_SERVICE);
// 设置桌面窗口参数
params.type = WindowManager.LayoutParams.TYPE_PHONE;
// 设置图片格式,效果为背景透明
params.format = PixelFormat.RGBA_8888;
// 设置桌面窗口的焦点
params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
// 桌面窗口的位置
params.gravity = Gravity.LEFT | Gravity.TOP;
// 设置位于屏幕的坐标
params.x = 0;
params.y = 0;
// 设置窗口的长,宽(根据需求)
params.width = 521;
params.height = 250;
// 加载窗口视图view
floatLayout = (LinearLayout) LayoutInflater.from(getApplication()).inflate(R.layout.float_window_view, null);
// 初始化桌面窗口组件
final LinearLayout rootView = (LinearLayout) floatLayout.findViewById(R.id.float_window_root_view);
// 设置其改变的位置
floatLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
// 设置桌面窗口的点击事件
rootView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// 获得触摸位置即当前屏幕上的坐标
params.x = (int) (event.getRawX() - rootView.getMeasuredWidth() / 2);
params.y = (int) (event.getRawY() - rootView.getMeasuredHeight() / 2);
// 拖动之后 刷新位置
manager.updateViewLayout(floatLayout, params);
// 必须返回false
return false;
}
});
// 设置桌面窗口的点击事件
TextView floatTv = (TextView) floatLayout.findViewById(R.id.float_window_close_tv);
floatTv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "点击了桌面窗口", Toast.LENGTH_SHORT).show();
// 之后停止桌面窗口服务
stopService(MainActivity.floatIntent);
}
});
// 添加桌面窗口
manager.addView(floatLayout, params);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
super.onDestroy();
if (floatLayout != null) {
manager.removeView(floatLayout);
}
}
}
之后,你想要在桌面上的窗口显示什么内容,就在布局文件中来吧!
<?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"
android:id="@+id/float_window_root_view"
android:background="#e48c00" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp" >
<TextView
android:id="@+id/float_window_close_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="5dp"
android:text="关闭" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:color/black" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_normal" />
</LinearLayout>
</LinearLayout>
我只是简单了设置了一个TextView和一个图片,TextView点击之后可以停止服务
好了,设置完毕! 为什么自己感觉这么low呢!