截屏分享:思路分析
1.监听截屏按键(第一种);
2.监听文件变化(第二种);
3.监听全局资源变化(第三种);
第一种肯定是不行的,各家厂商跟快捷键不一样,还有安卓版本不一样截屏按键也不一样;
第二种可以满足功能,但是有缺陷,就是适配效果不是那么好;
第三种百分90可以的,自己试过,截屏生成文件需要时间,一般三秒到五秒左右;看手机性能了;
我只贴第三种功能:
public class FileContextObserver extends AppCompatActivity { private static final String[] KEYWORDS = { "screenshot", "screen_shot", "screen-shot", "screen shot", "screencapture", "screen_capture", "screen-capture", "screen capture", "screencap", "screen_cap", "screen-cap", "screen cap","Pictures","screenshots","pictures" ,"Screenshots" }; /** 读取媒体数据库时需要读取的列 */ private static final String[] MEDIA_PROJECTIONS = { MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DATE_TAKEN, }; /** 内部存储器内容观察者 */ private ContentObserver mInternalObserver; /** 外部存储器内容观察者 */ private ContentObserver mExternalObserver; private HandlerThread mHandlerThread; private Handler mHandler; private ImageView obsevre_imageView; private Bitmap bm; private Handler mFileObserver=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what==1){ Log.d("otouzi", data +"第msg个"); obsevre_imageView.setImageBitmap(bm); } } }; protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_file_context_observer); obsevre_imageView= (ImageView) findViewById(R.id.obsevre_imageView); mHandlerThread = new HandlerThread("Screenshot_Observer"); mHandlerThread.start(); mHandler = new Handler(mHandlerThread.getLooper()); // 初始化 mInternalObserver = new MediaContentObserver(MediaStore.Images.Media.INTERNAL_CONTENT_URI, mHandler); mExternalObserver = new MediaContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mHandler); // 添加监听 this.getContentResolver().registerContentObserver( MediaStore.Images.Media.INTERNAL_CONTENT_URI, false, mInternalObserver ); this.getContentResolver().registerContentObserver( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, false, mExternalObserver ); } protected void onDestroy() { super.onDestroy(); // 注销监听 this.getContentResolver().unregisterContentObserver(mInternalObserver); this.getContentResolver().unregisterContentObserver(mExternalObserver); } private void handleMediaContentChange(Uri contentUri) { Cursor cursor = null; try { // 数据改变时查询数据库中最后加入的一条数据 cursor = this.getContentResolver().query( contentUri, MEDIA_PROJECTIONS, null, null, MediaStore.Images.ImageColumns.DATE_ADDED + " desc limit 1" ); if (cursor == null) { return; } if (!cursor.moveToFirst()) { return; } // 获取各列的索引 int dataIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); int dateTakenIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATE_TAKEN); // 获取行数据 String data = cursor.getString(dataIndex); long dateTaken = cursor.getLong(dateTakenIndex); Log.d("otouzi", data + " " + dateTaken+"第四个"); // 处理获取到的第一行数据 handleMediaRowData(data, dateTaken); } catch (Exception e) { e.printStackTrace(); } finally { if (cursor != null && !cursor.isClosed()) { cursor.close(); } } } /** * 处理监听到的资源 */ private void handleMediaRowData(final String data, long dateTaken) { if (checkScreenShot(data, dateTaken)) { mFileObserver.postDelayed(new Runnable() { @Override public void run() { bm = BitmapFactory.decodeFile(data); mFileObserver.sendEmptyMessage(1); } },3000); Log.d("otouzi", data + " " + dateTaken+"第一个"); } else { Log.d("otouzi", "Not screenshot event"+"第二个"); } } /** * 判断是否是截屏 */ private boolean checkScreenShot(String data, long dateTaken) { data = data.toLowerCase(); // 判断图片路径是否含有指定的关键字之一, 如果有, 则认为当前截屏了 for (String keyWork : KEYWORDS) { if (data.contains(keyWork)) { return true; } } return false; } /** * 媒体内容观察者(观察媒体数据库的改变) */ private class MediaContentObserver extends ContentObserver { private Uri mContentUri; public MediaContentObserver(Uri contentUri, Handler handler) { super(handler); mContentUri = contentUri; } @Override public void onChange(boolean selfChange) { super.onChange(selfChange); Log.d("otouzi", mContentUri.toString()+"第三个"); // obsevre_imageView.setImageURI(mContentUri); handleMediaContentChange(mContentUri); } } }