android-service

Most concise way to run a simple background task?

偶尔善良 提交于 2019-12-10 16:06:24
问题 I've seen at least five patterns through which you can have some code to run in a worker thread. Most simple: new Thread(new Runnable() { public void run() { // } }).start(); We can extend AsyncTask ; we have AsyncTaskLoader and other Loader s for data, Service s and so on. I assume each one of those has some advantages which I'm not discussing here. What I'm wondering is: which is the most appropriate, concise way to run a simple action? Defining simple : Quite short action, <1s; No need to

Is there a need to have one ServiceConnection per each Service bind?

拟墨画扇 提交于 2019-12-10 15:38:58
问题 I have several Android Service s that I want to bind to in my Activity , so I can monitor several actions from the user. To be able to bind every Service, and I will have several, do I need several private ServiceConnection s in my activity like the following? /** Defines callbacks for service binding, passed to bindService() */ private ServiceConnection mConnection = new ServiceConnection() { @Override public void onServiceConnected(ComponentName className, IBinder service) { // We've bound

Android USB Host and Device Sleep

你说的曾经没有我的故事 提交于 2019-12-10 14:41:32
问题 I have a USB device that I am interfacing with an android app using USB host mode. The device sends some data approximately once every 10 seconds but in some cases (high priority data) the data can come anytime. On the android app side I have a service that reads the data using USB host mode API. My android app processes the data and writes data back to the usb device. Now all works fine till the android device goes to sleep. The service goes to pause and all communication stops. I understand

How to get the service object inside activities?

↘锁芯ラ 提交于 2019-12-10 13:49:24
问题 This the code of my service: public class DownloadService extends Service { LocalBroadcastManager mLocalBroadcastManager; ArrayList<DownloadAsyncTask> dat = new ArrayList<DownloadAsyncTask>(); @Override public IBinder onBind(Intent arg0) { return null; } @Override public void onStart( Intent intent, int startId ) { super.onStart( intent, startId ); Log.d("service","DownloadService started"); } public void startDownload(String url, String fname, ProgressBar pBar){ int dID = dat.size(); Log.d(

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

Android service worker thread launches activity and waits a response

怎甘沉沦 提交于 2019-12-10 11:39:12
问题 I have a service that starts on boot and should run while the device is on. This service has a worker thread that launches an activity (QueryActivity) when a certain event takes place. This activity is launched trough an intent: private void launchActivity(String msg){ Intent intent = new Intent(getBaseContext(), QueryActivity.class); intent.putExtra("query", msg); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplication().startActivity(intent); } The activity will display a text view

Android - Create a background service to receive PubNub messages

。_饼干妹妹 提交于 2019-12-10 11:06:48
问题 I am using PubNub API to get real time messages. I have implemented code to subscribe to my channel and receive ongoing messages. I just want these messages to receive in background even if my application is not open. I have learned about services and broadcast receiver in android but I am not understanding the how to use this with PubNub. Is there any proper way to achieve this? PubNub Subscribe Code public static final String GLOBAL_CHANNEL = "NAME_OF_MY_CHANNEL"; public void subscribe() {

Get TextView from RemoteViews to make its text scrollable

喜你入骨 提交于 2019-12-10 10:55:25
问题 I have a notification built with a RemoteView from a service. The RemoteView layout contains a TextView that I want to have it scrolling. I've set it up as marquee in the xml, all good. But now I need to set textView.setSelected(true) therefore I need to somehow reference the textView. So how do I get the textView from the RemoteView to do setSelected()? Or is there another way to make it scrolling in the notification? I tried LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT

Running a service in separate thread and waking it every 10 minutes?

无人久伴 提交于 2019-12-10 10:28:34
问题 My app will access a webservice to access data (even if the application is not in foreground), every 10 minutes. What is the best way to do that? First do I need to start my service in seaparate thread? And how to make it get update from server every 10 minutes? Some people said handler.postdelayed and some using Alarm Manager. which one is better and do we have some examples. 回答1: If the updates will occur while your application is running, you can use a Timer, as suggested in other answers,

Oreo: startService() doesn't throw IllegalStateException when called in background

偶尔善良 提交于 2019-12-10 10:24:09
问题 According to the background execution limits introduced in Android Oreo, calling startService when the app is in background should throw an IllegalArgumentException . Check this question: Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent. I created a sample app targetting Android Oreo that does the following: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Handler()