[安卓] 10、悬浮窗与获取其他任务信息

 

 

这里介绍的是一个浮动窗口以及用该浮动窗口显示当前在顶层的应用信息~一个需要写2个layout和2个java


 

在MainActivity中主要是那两个按钮的的监听事件,通过Intent启动和关闭另一个activity.

 1 public class MainActivity extends Activity {
 2     @Override
 3     public void onCreate(Bundle savedInstanceState) {
 4         super.onCreate(savedInstanceState);
 5         setContentView(R.layout.main);
 6         // FloatingWindowActivity的布局视图按钮
 7         Button start = (Button) findViewById(R.id.start_id);
 8         Button remove = (Button) findViewById(R.id.remove_id);
 9         start.setOnClickListener(new OnClickListener() {
10             @Override
11             public void onClick(View v) {
12                 // TODO Auto-generated method stub
13                 Intent intent = new Intent(MainActivity.this, FxService.class);
14                 startService(intent);
15                 finish();
16             }
17         });
18 
19         remove.setOnClickListener(new OnClickListener() {
20             @Override
21             public void onClick(View v) {
22                 Intent intent = new Intent(MainActivity.this, FxService.class);
23                 stopService(intent);
24             }
25         });
26 
27     }
28 }

 

另一个FxService则是悬浮窗的全部实现:其中核心都是在createFloatView()函数中实现的,因为在onCreate中调用的是这个函数。在该函数中首先对悬浮窗的一些属性进行设置,然后写其触摸和点击事件,触摸事件中主要是移动悬浮窗位置;点击事件主要是获取上层活动activity的packageName[80-82],然后根据包名获得进程名[84-86],下面就是根据获得的信息来更新悬浮窗的显示了并toast.

 1 private void createFloatView() {
 2     wmParams = new WindowManager.LayoutParams();
 3     // 获取WindowManagerImpl.CompatModeWrapper
 4     mWindowManager = (WindowManager) getApplication().getSystemService(
 5             getApplication().WINDOW_SERVICE);
 6     // 设置window type
 7     wmParams.type = LayoutParams.TYPE_PHONE;
 8     // 设置图片格式,效果为背景透明
 9     wmParams.format = PixelFormat.RGBA_8888;
10     // 设置浮动窗口不可聚焦(实现操作除浮动窗口外的其他可见窗口的操作)
11     wmParams.flags =
12     // LayoutParams.FLAG_NOT_TOUCH_MODAL |
13     LayoutParams.FLAG_NOT_FOCUSABLE
14     // LayoutParams.FLAG_NOT_TOUCHABLE
15     ;
16 
17     // 调整悬浮窗显示的停靠位置为左侧置顶
18     wmParams.gravity = Gravity.LEFT | Gravity.TOP;
19 
20     // 以屏幕左上角为原点,设置x、y初始值
21     wmParams.x = 0;
22     wmParams.y = 0;
23 
24     /*
25      * // 设置悬浮窗口长宽数据 wmParams.width = 200; wmParams.height = 80;
26      */
27 
28     // 设置悬浮窗口长宽数据
29     wmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
30     wmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
31 
32     LayoutInflater inflater = LayoutInflater.from(getApplication());
33     // 获取浮动窗口视图所在布局
34     mFloatLayout = (LinearLayout) inflater.inflate(R.layout.float_layout,
35             null);
36     // 添加mFloatLayout
37     mWindowManager.addView(mFloatLayout, wmParams);
38 
39     Log.i(TAG, "mFloatLayout-->left" + mFloatLayout.getLeft());
40     Log.i(TAG, "mFloatLayout-->right" + mFloatLayout.getRight());
41     Log.i(TAG, "mFloatLayout-->top" + mFloatLayout.getTop());
42     Log.i(TAG, "mFloatLayout-->bottom" + mFloatLayout.getBottom());
43 
44     // 浮动窗口按钮
45     mFloatView = (Button) mFloatLayout.findViewById(R.id.float_id);
46     mFloatLayout.measure(View.MeasureSpec.makeMeasureSpec(0,
47             View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
48             .makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
49     Log.i(TAG, "Width/2--->" + mFloatView.getMeasuredWidth() / 2);
50     Log.i(TAG, "Height/2--->" + mFloatView.getMeasuredHeight() / 2);
51     // 设置监听浮动窗口的触摸移动
52     mFloatView.setOnTouchListener(new OnTouchListener() {
53 
54         @Override
55         public boolean onTouch(View v, MotionEvent event) {
56             // TODO Auto-generated method stub
57             // getRawX是触摸位置相对于屏幕的坐标,getX是相对于按钮的坐标
58             wmParams.x = (int) event.getRawX()
59                     - mFloatView.getMeasuredWidth() / 2;
60             // Log.i(TAG, "Width/2--->" + mFloatView.getMeasuredWidth()/2);
61             Log.i(TAG, "RawX" + event.getRawX());
62             Log.i(TAG, "X" + event.getX());
63             // 25为状态栏的高度
64             wmParams.y = (int) event.getRawY()
65                     - mFloatView.getMeasuredHeight() / 2 - 25;
66             // Log.i(TAG, "Width/2--->" + mFloatView.getMeasuredHeight()/2);
67             Log.i(TAG, "RawY" + event.getRawY());
68             Log.i(TAG, "Y" + event.getY());
69             // 刷新
70             mWindowManager.updateViewLayout(mFloatLayout, wmParams);
71             return false;
72         }
73     });
74 
75     mFloatView.setOnClickListener(new OnClickListener() {
76         @Override
77         public void onClick(View v) {//显示当前是哪个应用在顶层
78             // TODO Auto-generated method stub
79             pm = getPackageManager();
80             am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
81             String packageName = am.getRunningTasks(1).get(0).topActivity
82                     .getPackageName();
83             try {
84                 String progressName = pm.getApplicationLabel(
85                         pm.getApplicationInfo(packageName,
86                                 PackageManager.GET_META_DATA)).toString();
87                 mFloatView = (Button) mFloatLayout
88                         .findViewById(R.id.float_id);
89                 mFloatView.setText(progressName);
90                 Toast.makeText(FxService.this, progressName,
91                         Toast.LENGTH_SHORT).show();
92             } catch (NameNotFoundException e) {
93                 // TODO Auto-generated catch block
94                 e.printStackTrace();
95             }
96         }
97     });
98 }

注意:如果想获得系统的TopActivity,还必须获得权限:

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

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

 

 

本文链接:http://www.cnblogs.com/zjutlitao/p/4231525.html

更多精彩:http://www.cnblogs.com/zjutlitao/

github:   https://github.com/beautifulzzzz/Android/tree/master/FloatingWindow

 

posted @ 2015-01-18 10:50  beautifulzzzz  阅读(1974)  评论(0编辑  收藏  举报