I have a heavy-duty init process that I would like to run in 2 different places, depending on how the application started.
The heavy-init runs in background with an AsyncTask.
The cases are:
if the application was started from a Widget (via Intent), I need to make the heavy init inside the Application.onCreate
in any other cases, I need to make the heavy init inside the Activity.onStart
In other words, I'd like to know if inside the Application.onCreate there's a way to retrieve the triggering Intent.
thanks
Fabio
As it is reffeered in this link : Android- How to check the type of Intent Filters Programmatically?
You can use the getAction() of your intent to know what type of intent launched the activity.
Related
I have a pretty large android application, all works well, unless when i get out of my application (onpause?) and when i try to get into my (not closed) application later.
So when i open it to continue where i started it throws a nullpointer exception or an inflate exception.
So where should i implement what?
OnCreate: only inflate layout elements?
OnStart: Assign values to inflated elements?
OnResume: do the same as in OnStart?
OnPause: should i save the value of my intent?
For example, one activity is all based on a certain ID that it will give in it's intent. At on create, he looks for his intent, and then sets the value of the intent and uses that to make some calls.
When i start the application at some later point, will he call the OnCreate (if the application window is not in RAM anymore?)? and when he does will OnCreate still have the intent (with values in there) with which it was called with?
There is a lot of documentation and training about this on the Android Developer website
I have a networking app that needs to update the data counter in the app's UI as bytes are being sent and received simultaneously. Have tried implementing an intent which carries the code this way:
intent.putExtra("received", last_rx)
intent.putExtra("sent", last_tx)
The issue is I can't call setResult() as the method where this intent is placed is not a View meaning I can't get the intents from onActivityResult. Is there a workaround or a better way to update my UI.
Please note that I call the last_rx and last_tx from other classes as a double like this::
myclass.last_rx += s.length();
I think you should try EventBus. It won't break your logic. EventBus a really nice, easy to use "publish/subscribe event bus optimized for Android".
If you really want to use intents for communication you have 2 options:
start activity with "singleTask" or "singleInstance" mode. New
activity wont be created and you get your intent in onNewIntent()
method. (bad way)
register local broadcast reciever in your activity. See docs how
to use it correctly.
I would like to stop an activity started with implicit intent(using Intent.ACTIN_CALL etc). Is there a way to do this?
Also when we use "startActivity()" function of Context(abstract class), how does it actually start it?
Thanks.
No, you cannot stop an activity.
To answer your second question, when you call startActivity() the Intent that you pass as a parameter is given to the Android framework. The framework performs the appropriate Intent resolution using the contents of the Intent to determine which activity to actually start. Then, depending on the activity that needs to be started, it may need to create a new OS process, instantiate the Application object for that application and finally instantiate the activity object and call onCreate() on it. However, there are other things that may or may not happen during this process, depending on the state of the task that contains the activity, the Intent flags used, the launchMode of the activity as defined in the manifest, etc.
Im still a bit new to the Android Service Class. I know you need to start the service from your application with startService(intent), however my problem is my service has methods inside it. I need to start the service with an intent and then create an object of that class in my Activity so I can call methods of the service. The problem is when I do this I create one instance of the service when I start it with an intent and another instance of the service when I create an object of the class in my activity. This means any data passed to the service from startService(intent) is not there when I create the object of the service. Any ways around this or am I just totally misusing the service class? I can give some code but its basically this:
//Create Object of ControlPanel service class.
ControlPanel cPanel = new ControlPanel();
//Create intent for starting ControlPanel service class
Intent controlPanel = new Intent(this, cPanel.getClass());
//Start Service
startService(controlPanel);
I'd say you are misusing the class :-).
Calling startService() multiple times does not result in starting multiple service.
From the doc:
Request that a given application service be started. The Intent can either contain the complete class name of a specific service implementation to start, or an abstract definition through the action and other fields of the kind of service to start. If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.
You should override onStartCommand() as well. The first startService call starts the service if it has not been started yet. In any case onStartCommand will intercept any further startService calls and the intent you want to send to it.
Any ways around this or am I just totally misusing the service class?
You are totally misusing the Service class.
A Service is used via two basic patterns:
Sending commands to it, via startService().
Binding to it, to call an API exposed by that Service, via bindService().
Binding more accurately depicts what you are trying to do ("so I can call methods of the service"), however binding is tricky to get right, particularly when it comes to configuration changes.
Hence, I would recommend first that you sit back and determine completely and precisely why you are using a Service in the first place. ControlPanel, for example, is a name I would associate with a UI, not UI-less ("background") operations. Then and only then can you determine if the command or the binding pattern is appropriate for your use case.
You can do this with one service class.
public static int myStaticMethod(){
return 1;
}
Simply make the methods you need to access static.
I have to publish the progress from a background service on the UI continuously on a progress bar. Any ideas on how to go about it. Intents won't work I guess coz they can only send the data once the activity is started. Ant other suggestions?
Update : The progress on the UI happens on a progress Bar
Extend Application, which is created once for entire application.
When Activity starts, store its reference to a field in your Application object. (Note that you can access Application using Activity.getApplication). Set this field to Activity reference or null in onPause/onResume calls.
Then in Service, you have also access to your Application by Service.getApplication. So look if your Activity reference is non-null, meaning that your Activity is shown to user, and update UI as needed in such case, by calling methods on your Activity.
Thanks for the help mice, but since I needed to update progress bars on different activities in my app depending on which one was visible, I found it easier to implement through Broadcast Intents and Recievers and Intent Filters. All I had to do was to Broadcast the progress in my service wrapped up in a bundle via a broadcast Intent (with a custom Intent Filter applied) and register (in onResume()) an inner subclass of BroadcastReciever in the activities which needed the progress (having the same intent filter). One can also unregister these recievers in the onPause() method of the activity to save memory headspace.