ScreenShot ile çekilen etkinliği ContentObserver
kullanarak tespit edebilirsiniz. Projelerimden birinde kullanıyorum.
ScreenShotContentObserver.java
public abstract class ScreenShotContentObserver extends ContentObserver {
private Context context;
private boolean isFromEdit = false;
private String previousPath;
public ScreenShotContentObserver(Handler handler, Context context) {
super(handler);
this.context = context;
}
@Override
public boolean deliverSelfNotifications() {
return super.deliverSelfNotifications();
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, new String[]{
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.DATA
}, null, null, null);
if (cursor != null && cursor.moveToLast()) {
int displayNameColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
String fileName = cursor.getString(displayNameColumnIndex);
String path = cursor.getString(dataColumnIndex);
if (new File(path).lastModified() >= System.currentTimeMillis() - 10000) {
if (isScreenshot(path) && !isFromEdit && !(previousPath != null && previousPath.equals(path))) {
onScreenShot(path, fileName);
}
previousPath = path;
isFromEdit = false;
} else {
cursor.close();
return;
}
}
} catch (Throwable t) {
isFromEdit = true;
} finally {
if (cursor != null) {
cursor.close();
}
}
super.onChange(selfChange, uri);
}
private boolean isScreenshot(String path) {
return path != null && path.toLowerCase().contains("screenshot");
}
protected abstract void onScreenShot(String path, String fileName);
}
Ve aktivitesinde bu sınıfı kullanın: -
public class MainActivity extends AppCompatActivity {
private ScreenShotContentObserver screenShotContentObserver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
screenShotContentObserver = new ScreenShotContentObserver(handler, this) {
@Override
protected void onScreenShot(String path, String fileName) {
File file = new File(path); //this is the file of screenshot image
}
};
}
@Override
public void onResume() {
super.onResume();
getContentResolver().registerContentObserver(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
true,
screenShotContentObserver
);
}
@Override
public void onPause() {
super.onPause();
try {
getContentResolver().unregisterContentObserver(screenShotContentObserver);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onDestroy() {
super.onDestroy();
try {
getContentResolver().unregisterContentObserver(screenShotContentObserver);
} catch (Exception e) {
e.printStackTrace();
}
}
}
bir Activity
ait veya
onDestroy
yöntemlerde
Observer
durdurmak unutmayın
Hey, sen teste ettin mi? d [bu çözüm] (http://stackoverflow.com/a/14951594/518179) FileObserver kullanarak? –
@renam nope .. Yakında kontrol edeceğim ve size bilgi vereceğim. – droidev
Sadece FYI Bence dosya gözlemcileri çalışacak ama bazı sürümleri android 6.0 ile birlikte buggy: https://stackoverflow.com/questions/36237314/fileobserver-and- contentobserver-not-çalışma-in-android-hatmi – Bqin1