android-handler

Execute long running operations on the service

南笙酒味 提交于 2019-11-29 16:15:05
I'm building an application which have a Service. I know that all application components run in the same UI process, at least you specify it in the manifest. So to avoid ANR's messages i have three ways. Specify the service in the manifest to run in a separate process like android:process=":remote" but i've read some StackOverflow's post that says that it's not a good idea, because it consume a lot of battery and cpu processing. That i really respect since those post are from trusted people. Use an IntentService. it's probably a good way out. but i need my service running even if the activity

How to avoid memory leaks due to custom static handler class?

ぐ巨炮叔叔 提交于 2019-11-29 14:09:21
I have certain memory leaks happening in my custom handler class ,but not sure how to fix it. checkedout a couple of examples online but nothing is specific to my code so not sure how to go about it : private val startupCallback = object: RetryCallback(NUMBER, DELAY) { override fun onRetry(retryCount: Int) { mySdkApi.applicationStartup(this) } override fun onCompleted(): Boolean { updateStatus(Callback.Status.StartUpSDK) return true } override fun onFailed(e: MyException?) { updateStatus(Callback.Status.StartUpSDK, "", e) } } Android studio keeps prompting "This handler class should be static

GIF type animation for marker in google map api ANDROID

你说的曾经没有我的故事 提交于 2019-11-29 03:59:42
I want to achieve a marker animation such as GIF animation. I got two images which should be blinking simultaneously. I found nothing which can acheive this in android. I am trying to do is , creating a handler which run every 1 second , and I am trying to set icon for marker. But it doesnt work. Please guide me in right direction. my code as of now is as follows. Handler handler = new Handler(); Boolean marker_color_bool = true; //adding marker and sending the marker instance to marker_animation() method where handler is called. MarkerOptions marker = new MarkerOptions() .title(delivery

Should I use AlarmManager or Handler?

▼魔方 西西 提交于 2019-11-29 02:46:11
问题 I'm writing an app that constantly polls the device's sensors and every so often should write down some statistics to a file. This could be as fast as once a second or as slow once a minute. Should I use Handler's postDelayed() method or just schedule it with the AlarmManager ? 回答1: I'd say that it depends on the polling interval. I guess it's quite low in your case (around a few secs), so you should go the Handler way, or by using the Timer class. AlarmManger is a much higher level service

Why use HandlerThread in Android

喜欢而已 提交于 2019-11-28 20:44:17
In android , Handler can be used to post / handle message, if I don't use a HandlerThread (pass its Looper to Handler), does that mean in this case Handler use MainThread (UI Thread) 's Looper ? What result will get if Handler uses MainThread's Looper ? May cause mainThread blocked ? You would use HandlerThread in case that you want to perform background tasks one at a time and you want that those tasks will run at the order of execution. For example if you want to make several network background operations one by one. Yes, the HandlerThread has it's own looper and handlers could be created

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

Execute long running operations on the service

旧时模样 提交于 2019-11-28 10:08:14
问题 I'm building an application which have a Service. I know that all application components run in the same UI process, at least you specify it in the manifest. So to avoid ANR's messages i have three ways. Specify the service in the manifest to run in a separate process like android:process=":remote" but i've read some StackOverflow's post that says that it's not a good idea, because it consume a lot of battery and cpu processing. That i really respect since those post are from trusted people.

How to avoid memory leaks due to custom static handler class?

眉间皱痕 提交于 2019-11-28 08:10:39
问题 I have certain memory leaks happening in my custom handler class ,but not sure how to fix it. checkedout a couple of examples online but nothing is specific to my code so not sure how to go about it : private val startupCallback = object: RetryCallback(NUMBER, DELAY) { override fun onRetry(retryCount: Int) { mySdkApi.applicationStartup(this) } override fun onCompleted(): Boolean { updateStatus(Callback.Status.StartUpSDK) return true } override fun onFailed(e: MyException?) { updateStatus

How to call a method in activity from a service

China☆狼群 提交于 2019-11-28 05:20:28
There is a service that listens for some voice. If voice matches a string a certain method is invoked in the service object. public class SpeechActivationService extends Service { public static Intent makeStartServiceIntent(Context pContext){ return new Intent(pContext, SpeechActivationService.class); } //... public void onMatch(){ Log.d(TAG, "voice matches word"); } //... } This is how I start the service in my activity: Intent i = SpeechActivationService.makeStartServiceIntent(this); startService(i); From this service method, how can I invoke a method that resides in the activity object? I

Handler is abstract ,cannot be instantiated

a 夏天 提交于 2019-11-28 03:17:39
I am trying to use a Handler in my app. But when i instantiate it like this: Handler handler = new Handler(); I get the following error. Gradle: error: Handler is abstract; cannot be instantiated And when i check the solutions, it asks me to implement these methods: Handler handler = new Handler() { @Override public void close() { } @Override public void flush() { } @Override public void publish(LogRecord record) { } }; I have never used Handlers before and i am using it just to call a method after some delay. To achieve that, I've used: handler.postDelayed(new Runnable() { @Override public