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?
Related
I'm trying to pass the data from one activity to another activity without opening the activity. I tried it by not writing startActivity(intent) part but it fails.
How do I pass the data from one activity(1) to another activity(2) without opening that activity(2)?
Activities are UI elements so your question makes no sense. If you want perform some action that does not have UI create a service to perform the work and start or otherwise call that service from your first activity.
If you want to make data available to activity 2 when it does start, pick a persistent storage mechanism and write the data there, then read the data when activity 2 opens.
https://developer.android.com/guide/topics/data/data-storage
Alternatively, you can create a custom Application and store the data there and share it between activities.
Just a word of advice. You should describe what you are trying to accomplish not how you want to accomplish it. Activities are not the right tool for this task, but we can't suggest the right tool because we don't know what you want to build.
You can use events for passing around data. Either use the android native LocalBroadcastsSytem and put data as parcelable in the intent, or you can use any event library like EventBus
Pass the data from one activity to another activity without opening the activity try to do with the broadcast receiver or SharePhreferance or make that data public static You can access your data in anywhere of the application.
I have an alarm(repeatingalarm) and BroadcastReceiver to handle it. the alarm is set in my activity with pending intent referring to BroadcastReceiver class. I have a sqlite database in my activity. i want to update my sqlite database in my alarm so i need sqlite object from my activity. it is needed for my asynctask too which is executed in my alarm too. how can i do that?
in short terms, i want to pass my activity object to my broadcast constructor so i can use my sqlite instance.
EDIT: I noticed that if i define BroadcastReciever inside my activity, i can refer to it as simple as MyActivity.this. is there any other way rather than this.
you can not do too much work in 'broadcastreceiver' .
Intend you must create one service class to do this and call service from 'broadcastreceiver' . and then write your DataBase code in Service .
In Service you can create 'sqlite' DataBase object through getApplicationConext().
Also within Service must create Seperate Thread for your work or use AsyncTask to do your DataBase Operations.
I am new to android programming. I want to create a very simple widget in which I just have a single button and on click of that button, I want to execute some code.
My question is, is it necessary to create an activity for this?
Or can I just extend AppWidgetProvider class and write execution code in onUpdate or onReceive method?
Yes it is necessary to create a class that extends activity to accomplish interaction with a button.
In theory the provider is enough to handle appwidgets. In practice you will always need some kind of configuration, which is an Activity.
It is also very likely that the code to run, is too much for onUpdate and onReceive. At this moment you'll send Intent's out to some Actvity or Service.
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()
I am trying to write a AsyncTask generic package. Till now, what I've done is call one activity from another send the instance in the Intent to that Activity. This activity is part of the AsyncTask which calls the AsyncLoader.execute() file. I am doing this so that I don't lose any data that the parent Activity sets on the layout.
The other way to do it would be to call the Intent and return the data from the AsyncActivity to the parent Activity. But, both of these methods are slower than implementing the AsyncTask in the parent activity.
But, that clutters up the code. Thus, is there a better way of implementing it?
It sounds like your tight-coupling between the activity and the AsyncTask is causing you issues that you're trying to overcome with a weird workaround.
In my experience the best way to design activities that need an AsyncTask is:
Keep your AsyncTask out of your activity, i.e. make a separate class
that extends AsyncTask. This allows you to reuse the AsyncTask
between multiple activities and make it easier to test.
If you need to return data back to your activity, use the listener and implement the listener on your activity. Then pass your listener to a class that creates the AsyncTask.
Passing of data between intents should be kept to a minimum, if you need to reuse the same AsyncTask from a separate activity you should follow the steps above and execute the task again. If you're going to be calling this through the lifecycle of the app, then consider using a service instead.