'android.content.Context.deleteDatabase()'的定义在哪里

时间:2015-07-06 04:16:11

标签: android android-sqlite

当我们要在android上删除sqlite数据库时,我们可以这样写:

MainActivity.this.deleteDatabase("CollectionDebug.db");

然后我在ContextWrapper.deleteDatabase()上看了源代码:

@Override
public boolean deleteDatabase(String name) {
    return mBase.deleteDatabase(name);
}

mBase的类型是Context,继续关注Context.deleteDatabase():

/**
 * Delete an existing private SQLiteDatabase associated with this Context's
 * application package.
 *
 * @param name The name (unique in the application package) of the
 *             database.
 *
 * @return True if the database was successfully deleted; else false.
 *
 * @see #openOrCreateDatabase
 */
public abstract boolean deleteDatabase(String name);

这是一种抽象的方法吗?

定义在哪里?

1 个答案:

答案 0 :(得分:0)

您可以调用SQLiteDatabase的deleteDatabase方法,该方法是静态的并在源代码中定义 - 它实际上可能与contextWrapper使用的方法相同。

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.0_r1/android/database/sqlite/SQLiteDatabase.java#SQLiteDatabase.deleteDatabase%28java.io.File%29

726 
727     public static boolean More ...deleteDatabase(File file) {
728         if (file == null) {
729             throw new IllegalArgumentException("file must not be null");
730         }
731 
732         boolean deleted = false;
733         deleted |= file.delete();
734         deleted |= new File(file.getPath() + "-journal").delete();
735         deleted |= new File(file.getPath() + "-shm").delete();
736         deleted |= new File(file.getPath() + "-wal").delete();
737 
738         File dir = file.getParentFile();
739         if (dir != null) {
740             final String prefix = file.getName() + "-mj";
741             File[] files = dir.listFiles(new FileFilter() {
742                 @Override
743                 public boolean More ...accept(File candidate) {
744                     return candidate.getName().startsWith(prefix);
745                 }
746             });
747             if (files != null) {
748                 for (File masterJournal : files) {
749                     deleted |= masterJournal.delete();
750                 }
751             }
752         }
753         return deleted;
754     }

编辑:它实际上与Contex实现(ContextImpl)使用的相同:

1153    @Override
1154    public boolean More ...deleteDatabase(String name) {
1155        try {
1156            File f = validateFilePath(name, false);
1157            return SQLiteDatabase.deleteDatabase(f);
1158        } catch (Exception e) {
1159        }
1160        return false;
1161    }

http://grepcode.com/file/repo1.maven.org/maven2/org.robolectric/android-all/5.0.0_r2-robolectric-0/android/app/ContextImpl.java