通过Android应用程序进行硬重置/数据擦除Android设备

时间:2018-07-12 10:45:08

标签: android reset android-cursor android-contentresolver

我正在开发一个Android应用程序,以编程方式擦除Android中的所有数据,包括照片,视频,联系人,消息,应用程序,通话记录,应用程序数据等。我希望仅通过Android应用程序而不是通过adb来实现。

我正在想的方法是设法清除电话中的数据并用一些垃圾文件或二进制数据1010重写内存。 (这只是一个主意!)

我要做的就是使用光标 getContentResolver 从设备删除照片,视频,图像,联系人,通话记录,消息。但是我们可以使用第三方数据恢复软件来恢复它。

我还使用设备管理员重置了设备,但这的缺点是可以通过它恢复数据。

因此,我所需要的只是哪种方法可以硬重置设备。

我使用了下面的代码。

 public static void deleteAllContacts(Context activity) {
        ContentResolver contentResolver = activity.getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        while (cursor.moveToNext()) {
            String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
            Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey);
            contentResolver.delete(uri, null, null);
        }
    }

    public static void deleteAllCallLog(Context activity) {
        activity.getContentResolver().delete(CallLog.Calls.CONTENT_URI, null, null);
    }

 /*   public static void deleteAllMessages(Context activity) {
        ContentResolver localContentResolver = activity.getContentResolver();
        Uri localUri = Uri.parse("content://sms");
        for (int i = localContentResolver.delete(Telephony.Sms.CONTENT_URI, null, null); i == 1; i = localContentResolver.delete(localUri, null, null)) {

        }

    }*/


    public static void deleteVideoFiles(Activity activity) {
        String[] videoProjection = {MediaStore.Video.Media._ID, MediaStore.Video.Media.DATA,
                MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.SIZE};
        Cursor videoCursor = activity.managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoProjection, null, null, null);
        int count = videoCursor.getCount();
        for (int i = 0; i < count; i++) {
            int indexID = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID);
            videoCursor.moveToPosition(i);
            String path = getVideoFilePath(videoCursor.getString(indexID), activity);
            Log.d("Video Path", path);
            File file = new File(path);
            boolean isDeleted = file.delete();
            Log.d("isDELETED", String.valueOf(isDeleted));
        }
        videoCursor.close();
    }

    public static void deleteAudioFiles(Activity activity) {
        String[] proj = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.SIZE};
        Cursor audioCursor = activity.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
        if (audioCursor != null) {
            if (audioCursor.moveToFirst()) {
                do {
                    int indexId = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
                    String path = getAudioFilePath(audioCursor.getString(indexId), activity);
                    Log.d("Audio Path", path);
                    File file = new File(path);
                    boolean isDeleted = file.delete();
                    Log.d("isDELETED", String.valueOf(isDeleted));
                } while (audioCursor.moveToNext());
            }
        }
        if (audioCursor != null) audioCursor.close();

    }


    public static void deleteImageFiles(Activity activity) {

        Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        String[] projection = {MediaStore.Images.ImageColumns.DATA};
        Cursor c = null;
        SortedSet<String> dirList = new TreeSet<String>();
        String[] directories = null;
        if (u != null) {
            c = activity.managedQuery(u, projection, null, null, null);
        }

        if ((c != null) && (c.moveToFirst())) {
            do {
                String tempDir = c.getString(0);
                tempDir = tempDir.substring(0, tempDir.lastIndexOf("/"));
                try {
                    dirList.add(tempDir);
                } catch (Exception e) {

                }
            }
            while (c.moveToNext());
            directories = new String[dirList.size()];
            dirList.toArray(directories);

        }

        for (int i = 0; i < dirList.size(); i++) {
            File imageDir = new File(directories[i]);
            File[] imageList = imageDir.listFiles();
            if (imageList == null)
                continue;
            for (File imagePath : imageList) {
                try {

                    if (imagePath.isDirectory()) {
                        imageList = imagePath.listFiles();

                    }
                    if (imagePath.getName().contains(".jpg") || imagePath.getName().contains(".JPG")
                            || imagePath.getName().contains(".jpeg") || imagePath.getName().contains(".JPEG")
                            || imagePath.getName().contains(".png") || imagePath.getName().contains(".PNG")
                            || imagePath.getName().contains(".gif") || imagePath.getName().contains(".GIF")
                            || imagePath.getName().contains(".bmp") || imagePath.getName().contains(".BMP")
                            ) {
                        Log.d("Absolute Path", imagePath.getAbsolutePath());
                        File file = new File(imagePath.getAbsolutePath());
                        boolean isDeleted = file.delete();
                        Log.d("iSDELETED", String.valueOf(isDeleted));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }


    }

    public static String getAudioFilePath(String contentId, Activity activity) {
        Uri theMediaUri = Uri.withAppendedPath(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, contentId);
        String[] projection = {MediaStore.Audio.Media.DATA};
        Cursor mCur = activity.getContentResolver().query(theMediaUri, projection, null, null, null);
        int column_index = mCur.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        mCur.moveToFirst();
        return mCur.getString(column_index);

    }


    public static String getVideoFilePath(String contentId, Activity activity) {
        Uri theMediaUri = Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentId);
        String[] projection = {MediaStore.Video.Media.DATA};
        Cursor mCur = activity.getContentResolver().query(theMediaUri, projection, null, null, null);
        int column_index = mCur.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        mCur.moveToFirst();
        return mCur.getString(column_index);
    }

    public static void deleteSMS(Context context) {
      /*  try {
            Uri uriSms = Uri.parse("content://sms/inbox");
            Cursor c = context.getContentResolver().query(uriSms, new String[]{"_id", "body"}, null, null, null);

            if (c != null && c.moveToFirst()) {
                do {
                    long id = c.getLong(0);
                    Log.d("Body : ", c.getString(1));
                    context.getContentResolver().delete(
                            Uri.parse("content://sms/" + id), null, null);
                    Log.e("Message:", "Message is Deleted successfully");
                } while (c.moveToNext());
            }

            if (c != null) {
                c.close();
            }
        } catch (Exception e) {
            Log.e("Exception", e.toString());
        }*/


        Cursor c =context.getContentResolver().query(Uri.parse("content://sms/"), new String[]{"_id", "body"}, null, null,null);
        try {
            while (c.moveToNext()) {
                int id = c.getInt(0);
          int isDeleted =  context.getContentResolver().delete(Uri.parse("content://sms/" + id), null, null);

          Log.d("IS DELETED" , String.valueOf(isDeleted));
            }

        }catch(Exception e){
            Log.e("Delete Sms","Error deleting sms",e);
        }finally {
            c.close();
        }

    }

0 个答案:

没有答案