intentservice

第11章 Android的线程和线程池

匿名 (未验证) 提交于 2019-12-02 23:55:01
除了Thread以外,在Android中可以扮演线程的角色还有很多,比如AsyncTask和IntentService,同时HandlerThread也是一种特殊的线程。 AsyncTask封装了线程池和Handler。HandlerThread是一种具有消息循环的线程,在它的内部可以使用Handler。IntentService是一个服务,IntentService内部采用HandlerThread来执行任务,当任务执行完毕后IntentService会自动退出。IntentService是一种服务,不容易被系统杀死从而可以尽量保证任务的执行,如果是一个后台线程,由于这个时候进程中没有活动的四大组件,那么这个进程的优先级就会降低,容易被系统杀死,这就是IntentService的优点。 从Android3.0开始系统要求网络访问必须在子线程执行,否则网络访问将会失败并抛出NetworkOnMainThreadException这个异常。 11.2.1 AsyncTask AsyncTask是一种轻量级的异步任务类,它可以在线程池中执行后台任务,然后把执行的进度和最终结果传递给主线程并在主线程中更新UI。 4个核心方法: onPreExecute() doInBackground(Params… params)在此方法中可以通过publishProgress方法来更新任务进度

BroadcastReceiver does not receive Broadcast from IntentService

岁酱吖の 提交于 2019-12-02 11:47:20
问题 I'm trying to send a broadcast from an IntentService to the activity that started it, this is how i register the Receiver in the activity: private BroadcastReceiver mInitializer; @Override protected void onCreate(Bundle savedInstanceState) { .... mInitializer = new InitializationReceiver(); IntentFilter initializer = new IntentFilter(); initializer.addAction(IntentConstants.Tasks.INITIALIZE); initializer.addAction(IntentConstants.Initialization.INITIALIZE_IS_FIRST_START); initializer

make an IntentService not sleep until it executes a handler.postDelayed runnable

懵懂的女人 提交于 2019-12-02 06:23:12
问题 In the onHandleIntent of my my IntentService class, I created handle containing a runnable which should be done after 20 seconds. Unfortunatly my service sleeps or is destroyed before this period. I tried also with the CountDownTimer, but i had the same problem. Do someone have any idea can I make the onHnadleIntent waiting? Thank you! This is the code: public class MyService extends IntentService { //... @Override protected void onHandleIntent(Intent workIntent) { Handler handler = new

BroadcastReceiver does not receive Broadcast from IntentService

倾然丶 夕夏残阳落幕 提交于 2019-12-02 04:37:58
I'm trying to send a broadcast from an IntentService to the activity that started it, this is how i register the Receiver in the activity: private BroadcastReceiver mInitializer; @Override protected void onCreate(Bundle savedInstanceState) { .... mInitializer = new InitializationReceiver(); IntentFilter initializer = new IntentFilter(); initializer.addAction(IntentConstants.Tasks.INITIALIZE); initializer.addAction(IntentConstants.Initialization.INITIALIZE_IS_FIRST_START); initializer.addAction("test"); registerReceiver(mInitializer, initializer); .... } private class InitializationReceiver

GCM push notification for background app causes crash

馋奶兔 提交于 2019-12-02 03:56:19
I stumbled about a problem to receive GCM messages if my app runs in background (Service). In my scenario, I don't receive the GCM message (please notice it is not about how to receive GCM in general like here ) and the ActivityManager kills the app. So I wonder if I have a conceptual misunderstanding or if it is a general problem. Background I have an Android application that runs in foreground (Activity) and background (Service). The application is attached to a persistent notification to make sure that the app continues to run even if the user opens the Android TaskManager and swipes the

How to Keep the CPU of android phone always awake?

二次信任 提交于 2019-12-02 02:42:24
I have been stuck on it for days. I need to execute a function every minute. This function is making a POST call from the App to the Server and is transferring the location of the user per minute. The location coordinates are transferred for few hours, however, after few hours the transfer of location coordinates to the Server stops on its own. I am making use of WakefulBroadcastReceiver and IntentService to make sure that the CPU stays awake. I am also making use of Alarm to make sure that the function is executed every minute. This is how my WakefulBroadcastReceiver looks like: public class

make an IntentService not sleep until it executes a handler.postDelayed runnable

一曲冷凌霜 提交于 2019-12-02 00:57:42
In the onHandleIntent of my my IntentService class, I created handle containing a runnable which should be done after 20 seconds. Unfortunatly my service sleeps or is destroyed before this period. I tried also with the CountDownTimer, but i had the same problem. Do someone have any idea can I make the onHnadleIntent waiting? Thank you! This is the code: public class MyService extends IntentService { //... @Override protected void onHandleIntent(Intent workIntent) { Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { Log.i("20 seconds later","I am

Subscribing or binding to an existing Intent service

旧街凉风 提交于 2019-12-01 18:20:02
I have an app that has an initial activty that list some files within a list view. When an item is clicked within the list it takes you to a detail activity of that specific file. In the detail view I have a button called download, when you click on download it starts a IntentService which sets off the file to be downloaded as such: downloadButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(AppDetailsActivity.this, AppDownloadService.class); intent.putExtra(Consts.APP_DOWNLOAD_RECEIVER_KEY, new DownloadReceiver(new Handler()));

handler.postDelayed is not working in onHandleIntent method of IntentService

隐身守侯 提交于 2019-12-01 17:52:21
final Handler handler = new Handler(); LOG.d("delay"); handler.postDelayed(new Runnable() { @Override public void run() { LOG.d("notify!"); //calling some methods here } }, 2000); The "delay" does shows in the log, but not others at all. And the method called in the run() is not called at all also. Can anyone help explain why this happens, am I doing anything wrong? The class that has this code extends IntentService, will this be a problem? ============================ UPDATE: I put this code in the class that extends IntentService . The only place I found it worked was in the constructor. But

handler.postDelayed is not working in onHandleIntent method of IntentService

試著忘記壹切 提交于 2019-12-01 16:38:40
问题 final Handler handler = new Handler(); LOG.d("delay"); handler.postDelayed(new Runnable() { @Override public void run() { LOG.d("notify!"); //calling some methods here } }, 2000); The "delay" does shows in the log, but not others at all. And the method called in the run() is not called at all also. Can anyone help explain why this happens, am I doing anything wrong? The class that has this code extends IntentService, will this be a problem? ============================ UPDATE: I put this code