I'm implementing a DownloadManager-like feature in an application I'm developing. I use AsyncTask to push the download to the background.
This AsyncTask is inside a service named for example DownloadService which is instantiated on one of my activities. So this means multiple downloads could be started from different instances of an activity.
What I want to do is to have a particular activity in which I can have all the currently running downloads show their progress and have button to pause or resume the download.
In case you want such thing, you can store in an Array all Async Tasks that get started in the service, and then check their status with http://developer.android.com/reference/android/os/AsyncTask.html#getStatus%28%29.
Hope this helps!
Instead of housing DownloadService inside an Activity, house it inside you app's Application class as a class member instead, as there will exist only one instance of this class per application. You can then retrieve it in you activity by calling ((YourApplication)getApplication()).getDownloadService(). You will have to:
Create a class that extends Application, say YourApplication
Declare it in the manifest file's <application> tag as android:name="YourApplication"
Use it in your activity by invoking ((YourApplication)getApplication()).someMethodToGetDownloadService()
Related
I need to implement very specific code in the start of the application.
I mean, not in the start of the activity(onCreate() or onStart()) but in the start of the application.
I had one solution which is not good for me, which is to have a base activity called "MyBaseActivity" and then extends from it in all of my activities.
This solution is not good for me, because this solution makes me to be able to do only one specific thing in the onCreate of each activity(the specific code I talked about), which is not what I want.
I want every activity to be able to do different things according to their onCreate() func, and in addition to do the specific code that I talked about above.
Therefor, I need to access the start of the application, or that you have another solution for me.
Thank you !
The Application class, or your subclass of the Application class, is instantiated before any other class when the process for your application/package is created.
You need to extend application class.
public class AppApplication extends Application{
#Override
public void onCreate() {
super.onCreate();
//Do whatever you want
}
}
And this AppApplication class should be included in manifest file.
<application
android:allowBackup="true"
android:name=".AppApplication"
android:icon="#mipmap/ic_launcher"
I need to implement very specific code in the start of the application.
Every time when Android "gets a request" to start any of your app component (Activity, Service, BroadcastReceiver) and your app isn't running yet, it forks the app_process (a.k.a zygote), changes its name to your.package.name defined in AndroidManifest.xml, initializes an Application instance, calls its onCreate() method, then instantiates the component requested and calls its lifecycle methods (Activity's onCreate(), Service's onCreate() or BroadcastReceiver's onReceive()).
There can be only single instance of Application class which lives untill the app process dies. That said, any class instances you create within your extended Application class will also live until the app process is killed by the system.
Example: Understanding the Android Application Class
I eveyone,
I have this doubt, I am creating a mediaplayer and I can create the notification with buttons in both, if I create in the MainActivity it will be more simple because many variable is over there, but if I create inside service I need call onStartCommand() many times and also need pass data through of intents.Then my Question is: what is the best option?
I am starting to write an application which uses a socket to communicate with another device. I am currently trying to design the architecture of the application. It will have multiple activities. My question is if I have a single socket instance that gets passed between activities, or is declared in a class which extends Application, how do I know when to close it unless I open and close it in each activity? - Although this would work I am unsure if it is the 'cleanest' method.
I don't think you will find another good way of doing what you want. I would create the object inside the application class or as a singleton. And maybe create a BaseActivity class which will get the reference in onCreate and release it on onDestroy. This class you would extend in every activity you want to use your object in.
I would also track the references inside the Singleton class that defines your "socket", meaning have a counter which is incremented on each get and decremented on each release call. when the counter gets to 0 you can close the socket or better post a runnable which will close it after a specific time if no new activities "get" the reference again.
I have done something similar in my library for handling runnable.
Check it out here:
BaseObservableThreadPoolService |
ThreadCountObserver
I am new to Android development. After learning from many tutorials I got many Activities and many Fragments. How can I make a core engine to check what Activity is running and what Fragment is showing on a container?
Assume that I have:
Acivity01, Activity02, ... , Activity10
Fragment01, Fragment02, ... , Fragment10
I want to make a class that filters the Activity where Activity is on runtime and what Fragment is embeded to that activity.
How can I do this?
If I understand you correctly, you may want to store some references within your Application class to an Activity and to Fragment instance(-s), which are currently in foreground (by this I mean that user can instantly interact with Activity/Fragment).
As for Activity
Create some Activity field in your Application class and getter/setter methods for it (e.g., setCurrentActivity(), getCurrentActivity()). Then call setCurrentActivity() from onResume() method for each of your Activity instances. Don't forget to call setCurrentActivity, supplying null reference to ir in order to properly handle a case, when there are no foreground activities, but application is stll working.
As for Fragment
The general idea is similar to the first item, but there can be more than one Fragment instance in foreground state at time. So you need to store something like List, where you add your resumed fragments and remove paused.
You may also want to implement something similar for dialogs, for example. Then use the same strategy. Hope it will help.
I have a class named PatientDetails in which i am storing the values from the xml and then need to access its variables and method from the service as well from the activity at the same point of time ??
This is a typical multi thread scenario. You can do it without any troubles as long as you are just reading the data.
If you are reading the data from patient details class through your activity and writing data to it through your service you will get into run time exceptions. You have carefully synchronize the variables or methods in such cases.
One way to share a 'helper' class is to hold a 'static' reference to a single instance of it in the Application component of your app. Example...
public class MyApp extends Application {
public static detailsHelper;
#Override
public void onCreate() {
detailsHelper = new PatientDetails();
}
}
When you need to use the 'helper' in any other component such as an Activity or Service you simply reference it by the Application name as follows...
MyApp.detailsHelper.doSomething();
Technically speaking, under default conditions there is no such occurrence of two components accessing something at the same time because an Android Application and all of its components exist within a single process with a single thread of execution.
You should be very careful, however, if any of the components execute code which uses threads. For example, an Activity using an AsyncTask or perhaps using an IntentService which creates its own worker thread to do work. In this case, make sure any methods in the 'helper' class which write data, do so in a thread-safe manner.