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(); }
Android java : How to copy/move file from assets to absolute path?
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
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 pass a Factory to create the ViewModel.
Implement your own Factory then you can have args constructor for your ViewModel.
Here we set up a ViewModel with 1 argument construcgtor.
Example code:
public QASViewModel(@NonNull FragmentActivity activity) { this.activity = activity; } QASViewModel qasViewModel = ViewModelProviders.of(getActivity(), new QASViewModelFactory(getActivity())).get(QASViewModel.class);
import androidx.annotation.NonNull; import androidx.fragment.app.FragmentActivity; import androidx.lifecycle.ViewModel; import androidx.lifecycle.ViewModelProvider; import java.lang.reflect.InvocationTargetException; public class QASViewModelFactory extends ViewModelProvider.NewInstanceFactory { private final FragmentActivity activity; /** * Creates a {@code AndroidViewModelFactory} * * @param activity an AssetManager to pass in {@link QASViewModel} */ public QASViewModelFactory(@NonNull FragmentActivity activity) { this.activity = activity; } @NonNull @Override publicT create(@NonNull Class modelClass) { if (QASViewModel.class.isAssignableFrom(modelClass)) { try { return modelClass.getConstructor(FragmentActivity.class).newInstance(activity); } catch (NoSuchMethodException e) { throw new RuntimeException("Cannot create an instance of " + modelClass, e); } catch (IllegalAccessException e) { throw new RuntimeException("Cannot create an instance of " + modelClass, e); } catch (InstantiationException e) { throw new RuntimeException("Cannot create an instance of " + modelClass, e); } catch (InvocationTargetException e) { throw new RuntimeException("Cannot create an instance of " + modelClass, e); } } return super.create(modelClass); } }
How to convert UTC Date Time String to Java object and compare it ?
import java.time.Instant;
Instant instantStart = Instant.parse("20200229T12:00:00Z");
Instant instantEnd = Instant.parse("20200329T12:00:00Z");
Instant instantNow = Instant.now();
return instantNow.isAfter(instantStart) &&
instantNow.isBefore(instantEnd);
fixed: embedded-redis: Unable to run on macOS Sonoma
Issue you might see below error while trying to run embedded-redis for your testing on your macOS after you upgrade to Sonoma. java.la...
-
F:\webrowser>react-native run-android Scanning folders for symlinks in F:\webrowser\node_modules (73ms) Starting JS server... Buildin...
-
Refer: https://github.com/bazelbuild/bazel/wiki/Building-with-a-custom-toolchain https://www.tensorflow.org/tutorials/image_recognition
-
Solution react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android...