特殊的需求造就特殊的功能
有的付費頁面是不能讓用戶截屏分享的,所以...我們就流氓的攔截掉并且刪除
實現(xiàn)方式
- 1,注冊一個媒體庫內(nèi)容變化的ContentObserver
- 2,獲取到媒體文件,查看創(chuàng)建時間和現(xiàn)在時間的差值
- 3,刪除文件并且通知媒體庫
代碼如下
/**
* Created by wangfei on 2016/8/1 19:02.
* 攔截用戶截屏,并且刪除截屏文件
*/
public class ScreenshotContentObserver {
private Context mContext;
private HandlerThread handlerThread;
private Handler handler;
private static ScreenshotContentObserver instance;
private static final String TAG = "WWW";
private static final String EXTERNAL_CONTENT_URI_MATCHER =
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString();
private static final String[] PROJECTION = new String[]{
MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.DATA,
MediaStore.Images.Media.DATE_ADDED
};
private static final String SORT_ORDER = MediaStore.Images.Media.DATE_ADDED + " DESC";
private ContentResolver mContentResolver;
private ContentObserver mContentObserver;
private ScreenshotContentObserver(Context context) {
mContext = context;
handlerThread = new HandlerThread("content_observer");
handlerThread.start();
handler = new Handler(handlerThread.getLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//刪除截屏
((File)msg.obj).delete();
// 最后通知圖庫更新
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile((File)msg.obj)));
}
};
}
public static void startObserve(Context context) {
if (instance == null) {
instance = new ScreenshotContentObserver(context);
}
instance.register();
}
public static void stopObserve() {
instance.unregister();
}
private void register() {
mContentResolver = mContext.getContentResolver();
mContentObserver = new ContentObserver(handler) {
@Override
public void onChange(boolean selfChange, Uri uri) {
Log.d(TAG, "onChange: " + selfChange + ", " + uri.toString());
if (uri.toString().matches(EXTERNAL_CONTENT_URI_MATCHER)) {
Cursor cursor = null;
try {
cursor = mContentResolver.query(uri, PROJECTION, null, null, SORT_ORDER);
if (cursor != null && cursor.moveToFirst()) {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
long dateAdded = cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED));
long currentTime = System.currentTimeMillis();
Log.d(TAG, "path: " + path + ", dateAdded: " + dateAdded + ", currentTime: " + currentTime);
Log.d(TAG, "matchPath(path): " + matchPath(path) + ", matchAddTime(dateAdded): " +
matchAddTime(dateAdded) + ", matchSize(path): " + matchSize(path));
if (matchPath(path) && matchAddTime(dateAdded)) {
// screenshot added!
File file = new File(path);
Message message=Message.obtain();
message.obj=file;
//解決華為手機無法刪除截屏問題
handler.sendMessageDelayed(message,250);
}
}
} catch (Exception e) {
Log.d(TAG, "Exception" + e.toString());
} finally {
if (cursor != null) {
cursor.close();
}
}
}
super.onChange(selfChange, uri);
}
};
mContentResolver.registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true,
mContentObserver);
}
private void unregister() {
mContext.getContentResolver().unregisterContentObserver(mContentObserver);
}
/**
* 添加時間與當(dāng)前時間不超過1.5s,大部分時候不超過1s瘟斜。
*
* @param addTime 圖片添加時間,單位:秒
*/
private boolean matchAddTime(long addTime) {
return System.currentTimeMillis() - addTime * 1000 < 1500;
}
/**
* 尺寸不大于屏幕尺寸
*/
private boolean matchSize(String filePath) {
int w = ScreenUtil.getScreenWidth(mContext);
int h = ScreenUtil.getScreenWidth(mContext);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
return w >= options.outWidth && h >= options.outHeight;
}
/**
* 已調(diào)查的手機截屏圖片的路徑中帶有screenshot
*/
private boolean matchPath(String filePath) {
String lower = filePath.toLowerCase();
return lower.contains("screenshot");
}
}
使用方式(Activity中)
protected void onStart() {
super.onStart();
ScreenshotContentObserver.startObserve(this);
}
protected void onStop() {
super.onStop();
ScreenshotContentObserver.stopObserve();
}
最重要的一步
加權(quán)限
android.permission.READ_EXTERNAL_STORAGE