时间:2022-11-18 10:10:53 | 栏目:Android代码 | 点击:次
上篇文章给大家介绍了Android实现打开手机淘宝并自动识别淘宝口令弹出商品信息功能,接下来通过本文给大家分享android简单版仿淘口令复制弹出框功能,希望对大家有所帮助!
使用Android系统的粘贴板管理服务及ClipboardManager通过addPrimaryClipChangedListener添加Listener来监听粘贴板的状态,很很简单的一个小功能~
1.首先创建一个Service在后台运行:
Intent intent = new Intent(this,MainService.class); startService(intent);
另外同时在OnResume()中获得粘贴板复制的内容,用于在APP未启动或者Service被关闭时重新启动APP来弹出口令窗口
@Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); ClipboardManager mClipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); Log.e("Copylistenerdemo", mClipboardManager.getPrimaryClip().getItemAt(0).getText().toString()); }
2.在Service管理粘贴板服务:
mClipboardManager = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); mClipboardManager.addPrimaryClipChangedListener(mPrimaryClipChangedListener);
3.在onPrimaryClipChanged()做想要的事情,例如弹出框:
使用WindowManager来显示弹出框
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); final View floatView = layoutInflater.inflate(R.layout.floater, null); final WindowManager mWindowManager = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); LayoutParams params = new WindowManager.LayoutParams(); params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;//系统内部错误提示,显示于所有内容之上 params.format = PixelFormat.RGBA_8888; params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE; //当窗口可以获得焦点(没有设置FLAG_NOT_FOCUSALBE选项)时,仍然将窗口范围之外的点设备事件(鼠标、触摸屏)发送给后面的窗口处理 params.width = WindowManager.LayoutParams.MATCH_PARENT; params.height = WindowManager.LayoutParams.WRAP_CONTENT; params.gravity = Gravity.LEFT | Gravity.TOP; params.x = 0; params.y = 0; mWindowManager.addView(floatView, params); ObjectAnimator animatorShow = ObjectAnimator.ofFloat(floatView, "alpha", 0.0f,1.0f); animatorShow.setDuration(500); animatorShow.start(); ObjectAnimator animatorHide = ObjectAnimator.ofFloat(floatView, "alpha", 1.0f,0.0f); animatorHide.setDuration(500); animatorHide.setStartDelay(3000); animatorHide.start();
点击弹出框,跳转activity
floatView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(MainService.this, "点击淘口令", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setClass(MainService.this, xxActivity.class); startActivity(intent); } });
很简单的小功能,不过应用的实际过程应该还会出现一些待解决的小问题
好了,下面看下淘口令原理:ClipBoard笔记
Clipboard是Android提供的一个系统服务,它提供了一个全局的剪贴板,让文字、图片、数据,在多App间共享成为可能。
Clipboard的基本使用,分为三步:
获得ClipboardManager:
ClipboardManager mClipboardManager = mClipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
Copy:
ClipData mClipData; String text = "hello world"; mClipData = ClipData.newPlainText("test", text); mClipboardManager.setPrimaryClip(mClipData);
Paste:
ClipData clipData = mClipboardManager.getPrimaryClip(); ClipData.Item item = clipData.getItemAt(0); String text = item.getText().toString();
搞定!
总结