2015-07-11 43 views
6

我是android開發新手。聽取android中的截圖動作

我正在製作一個應用程序,它會在android手機屏幕截圖時發生反應。我聽說android允許這樣的行爲可以通過廣播接收器檢測到,所以我已經通過android開發者文檔,here

我認爲框架開發人員忘了或沒有實現屏幕截圖的廣播代碼,因爲他們沒有在他們的文檔中列出這個動作。

有沒有其他的方法我可以聽截圖採取行動?

+0

嘿,你有沒有測試d [此解決方案](http://stackoverflow.com/a/14951594/518179)使用FileObserver? –

+0

@renam nope ..我會檢查它,並通知你很快 – droidev

+1

只是FYI我認爲文件觀察員將工作,但一些版本與它像android 6.0的buggy https://stackoverflow.com/questions/36237314/fileobserver-and- contentobserver - 不工作功能於Android的棉花糖 – Bqin1

回答

2

您可以使用ContentObserver檢測ScreenShot採取的事件。我在我的一個項目中使用它。

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); 

} 

而且使用這個類在你的活動: -

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(); 
     } 
    } 

} 

不要忘記在Activity

的或 onDestroy方法停止 Observer