gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:02:37 25 4
gpt4 key购买 nike

当我们想在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);

这是一个抽象方法?!

定义在哪里?

最佳答案

您可以调用 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 }

编辑:它实际上与上下文实现 (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

关于android - 'android.content.Context.deleteDatabase()'的定义在哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31237709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com