Posts

Showing posts from March, 2020

Java learning: possible lossy conversion from int to short

fault code short s = 5; s = s - 2; error: incompatible types: possible lossy conversion from int to short s = s - 2; ^ 1 error Correct code // why below line ok ? ask java compiler. s -= 2; s = (short)(s - 2); Java report error while c/c++ do not even display a warning message in fact, c++ do the same things like Java do: Automatic promotion of shot to int, but implicit the conversion.

Java learning: why float a = 5.6; compile error ?

why can't float a = 5.6; ? error: incompatible types: possible lossy conversion from double to float float a = 5.6; ^ 1 error Cause double is the default type of floating point number in java language. Fix float a = 5.6f;

Java learning: Hello World program with java

Recently, I begin to use java in my work. I used to c++ for more than 10 years. Programmer start every thing begin with Hello World public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } Java main function, must be in a public class public static void main(String[] args) {} Compile HelloWorld javac HelloWorld.java Execute HelloWorld java HelloWorld remember, not java HelloWorld.class otherwise, you will got below error Error: Could not find or load main class HelloWorld.class

Java learning : how many classes defined in java source, how many .class byte code files will be generated after compile via javac

One java source file generate one .class byte code file ? False. I thought one java source code file will generate only one .class byte code file, since I am a c++ programmer for about 10 years. The correct answer is how many classes defined in java source, how many .class byte code files will be generated after compile via javac public class HelloWorld { class HelloInternal { } } enum HelloEnum { } interface HelloInterface { } javac HelloWorld.java ls *.class HelloEnum.class HelloWorld$HelloInternal.class HelloInterface.class HelloWorld.class Java classes The classes can be any class, interface and enum. All java codes should be placed in the classes. otherwise you will get compile error class, interface, or enum expected In fact, enum is a class too, just a syntactic sugar.

Android java: How to display phonetic symbol with correct fonts via WebView ?

Image
First, Download lingoes font file Android default fonts can not display phonetic symbol. lingoes.ttf can, please download it. Second, Put lingoes.ttf to assets/font Last, Add "lingoes" font-family in css files under assets @font-face { font-family: 'lingoes'; src:url('file:///android_asset/fonts/lingoes.ttf') format('truetype'); font-weight: normal; font-style: normal; } Use "lingoes" as font-family for the phonetic symbol content Android java : How to load html files in assets via WebView?

Android java : How to load html files in assets via WebView?

Image
The base url for html files under the assets of your app is "file:///android_asset/" WebView wv = new WebView(context); wv.getSettings().setJavaScriptEnabled(true); wv.loadUrl("file:///android_asset/coca/coca_00001_the.html");

Android java : How to copy/move file from assets to absolute path?

private void moveAssets(String assets_file_path, String absolutePath) throws IOException { File wwwFile = new File(absolutePath); if (wwwFile.exists()) return; InputStream is = activity.getAssets().open(assets_file_path); byte[] buffer = new byte[is.available()]; is.read(buffer); is.close(); wwwFile.createNewFile(); FileOutputStream os = new FileOutputStream(wwwFile); os.write(buffer); os.close(); }

TOP 16515 words from Corpus of Contemporary American English

Image
Download for Android https://play.google.com/store/apps/details?id=com.lengerrong.coca

Android java: How to orientation activity layout automatically ?

Add ~ android:screenOrientation="fullSensor" ~ to the activity AndroidManifest.xml activity android:name=".MainActivity" android:screenOrientation="fullSensor"

shell bash scripts : check process existed or not, if not existed then restart it

Put below scripts in your ~/.bash_profile or ~/.bashrc It will auto run "unison" process if not running yet when you open a new bash shell every time. Change the process name "unison" for your case. :) auto_unison() { u=`ps aux | grep unison | grep -v grep | wc -l` if [ $u -eq 0 ] then unison > /dev/null 2>&1 & fi } auto_unison

Android how to detect/register language/locale change listener/receiver

The easy way is to register a BroadcastReceiver for Intent.ACTION_LOCALE_CHANGED . Example: change your ViewModel data when language/locale changed. public class QASViewModel extends ViewModel { @SuppressLint ( "StaticFieldLeak" ) private final FragmentActivity activity ; public QASViewModel ( @NonNull FragmentActivity activity ) { this . activity = activity ; setLangReceiver (); } private void setLangReceiver () { final QASViewModel qasViewModel = this ; final BroadcastReceiver langReceiver = new BroadcastReceiver () { @Override public void onReceive ( Context context , Intent intent ) { // do action when language change } }; activity . getApplicationContext (). registerReceiver ( langReceiver , new IntentFilter ( Intent . ACTION_LOCALE_CHANGED )); } }

Android ViewModel with ArgsConstructor via Custom ViewModelFactory

Android ViewModel is very useful. However ViewModel has no args constructor by default. Typical usage of ViewModel looks like: public class UserModel extends ViewModel { } final UserModel viewModel = ViewModelProviders . of ( this ). get ( UserModel . class ); Let's look the definition of ViewModelProviders.of method. /** * Creates a {@link ViewModelProvider}, which retains ViewModels while a scope of given Activity * is alive. More detailed explanation is in {@link ViewModel}. * * It uses the given {@link Factory} to instantiate new ViewModels. * * @param activity an activity, in whose scope ViewModels should be retained * @param factory a {@code Factory} to instantiate new ViewModels * @return a ViewModelProvider instance */ @NonNull @MainThread public static ViewModelProvider of ( @NonNull FragmentActivity activity , @Nullable Factory factory ) { Have you found it? We can pas