android-thread

How to properly approach threading with Room

泄露秘密 提交于 2021-02-19 02:39:59
问题 I am confused about Room and can not find an answer in the documentation. So, the library enforces using queries on a separate Thread, which is understandable. However, it seems that Delete queries are not included in this limitation and can be freely called from the UI Thread. They also always return a raw output value, without a chance to wrap it in an Observable. What is the correct way to use the delete call in Room then ? Should it be run on a separate Thread? If not, what about

How can I perform LiveData transformations on a background thread?

允我心安 提交于 2021-01-20 15:29:10
问题 I have a need to transform one type of data, returned by a LiveData object, into another form on a background thread to prevent UI lag. In my specific case, I have: MyDBRow objects (POJOs consisting of primitive long s and String s); a Room DAO instance emitting these via a LiveData<List<MyDBRow>> ; and a UI expecting richer MyRichObject objects (POJOs with the primitives inflated into e.g. date/time objects) so I need to transform my LiveData<List<MyDBRow>> into a LiveData<List<MyRichObject>

start activity in custom UncaughtExceptionHandler class not working any more

ⅰ亾dé卋堺 提交于 2020-07-09 12:52:20
问题 I used to handle exception errors with this class and pass error as String to another activity for show , but this way not work any more , seems when kill process happened "sag.class" Activity cant launch any more public class ExceptionHandler extends MainActivity implements java.lang.Thread.UncaughtExceptionHandler { private final Context myContext; private SharedPreferences prefs; private StringBuilder errorReport; public ExceptionHandler(Context context) { myContext = context; } public

Android: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created

荒凉一梦 提交于 2019-12-20 05:52:05
问题 So inside a IntentService , the app maybe active or inactive , onHandleIntent gets called , where I have placed this below code.This is where I store the data into realm. Realm realm = null; try { realm = Realm.getDefaultInstance(); realm.executeTransactionAsync(new Realm.Transaction() { @Override public void execute(Realm realm) { for (AppItem item : items) { AppItem item2 = realm.createObject(AppItem.class, UUID.randomUUID().toString()); item2.mName = item.mName; item2.mCount = item.mCount;

I want to show gif image show on screen even when app is closed

六眼飞鱼酱① 提交于 2019-12-13 08:50:04
问题 With a mouse click, you implement two things: Sound is playing The GIF image is showing I'm trying to show the GIF on the screen even when I close the app I'm using the services but because the sound keeps playing while the GIF image appears, I can not find the solution. TheService.java (CODE) package gallery.suitapps.catwalking; import android.app.Service; import android.content.Context; import android.content.Intent; import android.media.MediaPlayer; import android.media.SoundPool; import

Android - How to download a large nos of images (large nos of http url) in Background Process

让人想犯罪 __ 提交于 2019-12-10 11:47:21
问题 I am getting a list of image url from server whose count is approximately in between 400-500. How can i download this image in background into local folder of the device ? SO far i have run a foreground service in which i am using ExecutorService to run a thread. My service code is below public class SaveImageService extends Service { private Context context; public static final String NOTIFICATION_CHANNEL_ID = "10001"; public SaveImageService() { } @Override public void onCreate() { super

When to use handler.post() & when to new Thread()

断了今生、忘了曾经 提交于 2019-11-28 15:38:48
I'm wondering when should I use handler.post(runnable); and when should I use new Thread(runnable).start(); It is mentioned in developers documentation for Handler: Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached. Does this mean if I write in the onCreate() of Activity class: Handler handler = new Handler(); handler.post(runnable); then runnable will be called in a separate thread or in the Activity's thread? kamituel You should use Handler.post() whenever you want to do operations in the UI thread. So let's say

When to use handler.post() & when to new Thread()

心不动则不痛 提交于 2019-11-27 00:00:46
问题 I'm wondering when should I use handler.post(runnable); and when should I use new Thread(runnable).start(); It is mentioned in developers documentation for Handler: Causes the Runnable r to be added to the message queue. The runnable will be run on the thread to which this handler is attached. Does this mean if I write in the onCreate() of Activity class: Handler handler = new Handler(); handler.post(runnable); then runnable will be called in a separate thread or in the Activity's thread? 回答1

Repeat a task with a time delay?

爱⌒轻易说出口 提交于 2019-11-25 22:27:39
问题 I have a variable in my code say it is \"status\". I want to display some text in the application depending on this variable value. This has to be done with a specific time delay. It\'s like, Check status variable value Display some text Wait for 10 seconds Check status variable value Display some text Wait for 15 seconds and so on. The time delay may vary and it is set once the text is displayed. I have tried Thread.sleep(time delay) and it failed. Any better way to get this done? 回答1: You