forked from Tencent/MMKV
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move MMKV init & destry logic into Application class
- Loading branch information
Showing
3 changed files
with
89 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
Android/MMKV/mmkvdemo/src/main/java/com/tencent/mmkvdemo/MyApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.tencent.mmkvdemo; | ||
|
||
import android.app.Application; | ||
import android.util.Log; | ||
|
||
import com.getkeepsafe.relinker.ReLinker; | ||
import com.tencent.mmkv.MMKV; | ||
import com.tencent.mmkv.MMKVContentChangeNotification; | ||
import com.tencent.mmkv.MMKVHandler; | ||
import com.tencent.mmkv.MMKVLogLevel; | ||
import com.tencent.mmkv.MMKVRecoverStrategic; | ||
|
||
public class MyApplication extends Application implements MMKVHandler, MMKVContentChangeNotification { | ||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
|
||
// set root dir | ||
//String rootDir = MMKV.initialize(this); | ||
String dir = getFilesDir().getAbsolutePath() + "/mmkv"; | ||
String rootDir = MMKV.initialize(dir, new MMKV.LibLoader() { | ||
@Override | ||
public void loadLibrary(String libName) { | ||
ReLinker.loadLibrary(MyApplication.this, libName); | ||
} | ||
}, MMKVLogLevel.LevelInfo); | ||
Log.i("MMKV", "mmkv root: " + rootDir); | ||
|
||
// set log level | ||
MMKV.setLogLevel(MMKVLogLevel.LevelInfo); | ||
|
||
// you can turn off logging | ||
//MMKV.setLogLevel(MMKVLogLevel.LevelNone); | ||
|
||
MMKV.registerHandler(this); | ||
MMKV.registerContentChangeNotify(this); | ||
} | ||
|
||
@Override | ||
public void onTerminate() { | ||
MMKV.onExit(); | ||
|
||
super.onTerminate(); | ||
} | ||
|
||
@Override | ||
public MMKVRecoverStrategic onMMKVCRCCheckFail(String mmapID) { | ||
return MMKVRecoverStrategic.OnErrorRecover; | ||
} | ||
|
||
@Override | ||
public MMKVRecoverStrategic onMMKVFileLengthError(String mmapID) { | ||
return MMKVRecoverStrategic.OnErrorRecover; | ||
} | ||
|
||
@Override | ||
public boolean wantLogRedirecting() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void mmkvLog(MMKVLogLevel level, String file, int line, String func, String message) { | ||
String log = "<" + file + ":" + line + "::" + func + "> " + message; | ||
switch (level) { | ||
case LevelDebug: | ||
Log.d("redirect logging MMKV", log); | ||
break; | ||
case LevelNone: | ||
case LevelInfo: | ||
Log.i("redirect logging MMKV", log); | ||
break; | ||
case LevelWarning: | ||
Log.w("redirect logging MMKV", log); | ||
break; | ||
case LevelError: | ||
Log.e("redirect logging MMKV", log); | ||
break; | ||
} | ||
} | ||
|
||
@Override | ||
public void onContentChangedByOuterProcess(String mmapID) { | ||
Log.i("content changed", mmapID); | ||
} | ||
} |