模仿某东商城的详情页面截屏会有弹窗事件的响应功能大体实现了,细节没做请不要纠结,进入正题。
对于系统级别截屏的监听,api中并没有针对于这个功能的监听回调等方法。我的首先想到的就是针对资源文件的监听。
思路如下:
1.对于系统级别的截图 肯定会保存到本地(机型不一样要注意适配,主要还是截图的命名方式不太一样)
2.对于资源文件监听时要判断是否是符合截图要求的(主要还是截图时间与当时时间差不能太大,否则会因为用户的其他操作影响到)
3.当检测到资源文件有变化查询最后一条并返回展示
话不多说上代码
MainActivity
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ImageView img;
private ScreenShot mScreenShot;
private TextView tvHelp;
private TextView tvShare;
private PopupWindow popupWindow;
private LinearLayout linearLayout;
private View showView;
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
linearLayout = findViewById(R.id.linearLayout);
showView = LayoutInflater.from(this).inflate(R.layout.layout_show_creenshot, null);
popupWindow = new PopupWindow(showView,
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
img = showView.findViewById(R.id.show_img);
tvHelp = showView.findViewById(R.id.tv_help);
tvShare = showView.findViewById(R.id.tv_share);
tvHelp.setOnClickListener(this);
tvShare.setOnClickListener(this);
popupWindow.setWidth(300);
popupWindow.setHeight(500);
}
private void initData() {
mScreenShot = ScreenShot.getInstance();
handelSystemScreenShot();
}
private void handelSystemScreenShot() {
if (!MainActivity.this.isFinishing()) {
mScreenShot.register(MainActivity.this, new ScreenShot.CallbackListener() {
@Override
public void onShot(String path) {
final String pa = path;
Log.i("tag", "onShot: 成功:" + path);
runOnUiThread(new Runnable() {
@Override
public void run() {
img.setImageURI(Uri.fromFile(new File(pa)));
popupWindow.showAtLocation(linearLayout, Gravity.LEFT,0,0);
}
});
}
});
} else {
mScreenShot.unregister();
}
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.tv_help){
Toast.makeText(MainActivity.this,"客服",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this,"分享",Toast.LENGTH_LONG).show();
}
}
@Override
protected void onDestroy() {
mScreenShot.unregister();
super.onDestroy();
}
}
main布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="80dp"
android:background="@mipmap/ic_launcher"
/>
</LinearLayout>
ScreenShot 类
import android.content.ContentResolver;
import android.content.Context;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.provider.MediaStore;
/**
* 系统截屏监听工具,监听系统截屏,然后对截图进行处理
*/
public class ScreenShot {
private static final String TAG = "ScreenShot";
private static final String[] MEDIA_PROJECTIONS = {
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.DATE_ADDED
};
/**
* 截屏依据中的路径判断关键字
*/
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"
};
private ContentResolver mContentResolver;
private CallbackListener mCallbackListener;
private MediaContentObserver mInternalObserver;
private MediaContentObserver mExternalObserver;
private static ScreenShot mInstance;
private ScreenShot() {
}
/**
* 获取 ScreenShot 对象
*
* @return ScreenShot对象
*/
public static ScreenShot getInstance() {
if (mInstance == null) {
synchronized (ScreenShot.class) {
mInstance = new ScreenShot();
}
}
return mInstance;
}
/**
* 注册
*
* @param context 上下文
* @param callbackListener 回调监听
*/
public void register(Context context, CallbackListener callbackListener) {
mContentResolver = context.getContentResolver();
mCallbackListener = callbackListener;
HandlerThread handlerThread = new HandlerThread(TAG);
handlerThread.start();
Handler handler = new Handler(handlerThread.getLooper());
mInternalObserver = new MediaContentObserver(MediaStore.Images.Media.INTERNAL_CONTENT_URI, handler);
mExternalObserver = new MediaContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, handler);
mContentResolver.registerContentObserver(MediaStore.Images.Media.INTERNAL_CONTENT_URI,
true, mInternalObserver);
mContentResolver.registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
true, mExternalObserver);
}
/**
* 注销
*/
public void unregister() {
if (mContentResolver != null) {
mContentResolver.unregisterContentObserver(mInternalObserver);
mContentResolver.unregisterContentObserver(mExternalObserver);
}
}
private void handleMediaContentChange(Uri uri) {
Cursor cursor = null;
try {
// 数据改变时,查询数据库中最后加入的一条数据
cursor = mContentResolver.query(uri, 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);
// 处理获取到的第一行数据
handleMediaRowData(cursor.getString(dataIndex));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
}
/**
* 处理监听到的资源
*/
private void handleMediaRowData(String path) {
long duration = 0;
long step = 100;
// 设置最大等待时间为1s,因为魅族的部分手机保存截图有延迟
while (!checkScreenShot(path) && duration <= 1000) {
try {
duration += step;
Thread.sleep(step);
} catch (Exception e) {
e.printStackTrace();
}
}
if (checkScreenShot(path)) {
if (mCallbackListener != null) {
//回调地址
mCallbackListener.onShot(path);
}
}
}
private boolean checkScreenShot(String path) {
if (path == null) {
return false;
}
path = path.toLowerCase();
for (String keyword : KEYWORDS) {
//判断是否符合 截图命名规则(过滤)
if (path.contains(keyword)) {
return true;
}
}
return false;
}
/**
* 媒体内容观察者
*/
private class MediaContentObserver extends ContentObserver {
private Uri mUri;
MediaContentObserver(Uri uri, Handler handler) {
super(handler);
mUri = uri;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
//Log.d("ScreenShot", "图片数据库发生变化:" + selfChange);
handleMediaContentChange(mUri);
}
}
/**
* 回调监听器
*/
public interface CallbackListener {
/**
* 截屏
*
* @param path 图片路径
*/
void onShot(String path);
}
}
弹窗
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:background="@color/colorBlack"
android:orientation="vertical">
<ImageView
android:id="@+id/show_img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_share"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorBlack"
android:gravity="center"
android:text="分享页面"
android:textColor="@color/colorWrite" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorWrite" />
<TextView
android:id="@+id/tv_help"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorBlack"
android:gravity="center"
android:text="问问客服"
android:textColor="@color/colorWrite" />
</LinearLayout>
</RelativeLayout>
关于这个还有几个点需要注意
由于是对系统资源变化的监听 所以会导致以下的问题
1.由于机型 厂商 版本等问题影响 会导致截屏图片 存储图片消耗的时间不同
2.删除图片资源时 也可能会调用 回调接口
解决:查询图片时也对图片的时间进行判断 目前暂定3秒(除人为极限操作)能够满足图片插入时间和删除回调的时间判断
系统在程序外截图也会通过过滤被程序检测到
1.添加过滤 因为系统截图时会自动带有截屏时间 截屏程序 以及存放位置 增加对于截屏程序包名过滤
已经找到和某东详情截图相同的方法了 已更新
码字不易,点赞收藏谢谢!!!