Activity necessary for Widget? - android

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.

Related

Is it good create notification inside service or main activity

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?

Changing an Activity's TextView in a BroadcastReceiver (Android)

I have a simple Android app with a single Activity that contains a TextView. I created a BroadcastReceiver whose purpose is to perform some numerical calculations, convert the result into a String, and then change the TextView's text value to that String. However, since the BroadcastReceiver is usually called when the App's Activity doesn't exist, I can't seem to find a way to access the Activity's TextView (I always get a NullPointerException).
I've read other articles here on StackOverflow that suggest implementing a Listener for my BroadcastReceiver with a callback method, but I'm honestly not sure how to implement this. I'm somewhat familiar with the concept of a callback method, I'm just not sure how to implement it in this context. Can anyone please suggest how I might accomplish this? Thank you!
EDIT
Using an AsyncTask would do to perform operations in background. Example one Example two
Note that you have to pass your TextView to the AsyncTask inside its constructor

Global Broadcast Receiver (Android)

I have an application with several activities and I want to be notified when the phone goes online (doesn't matter which activity is the user in). I've found a Broadcast Receiver to do it, but I'd like to know if there is a way to declare this BroadcastReceiver only once, I want to avoid having to place the code in each activity (I have more than 20 activities!).
Thank you.
Create a parent abstract class which extends Activity and define the broadcast receiver implementation there. Later, modify your activity classes to extend that parent class.
As in documentation, If we declares the broadcast receiver in manifest file then it automatically runs behind.
Thing is that now you need to handle the onReceive() method of the receiver.
Another way is that you make a separate java class and import wherever you want.

Is this the best way to implement AsyncTask? Or are there better ways?

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.

Sending an Activity to a non-Android class

I'm pretty much a noob when it comes to Android development. I have an Activity that has a method that pretty much just sets the text of a TextView to whatever text is provided as an argument. I have a second class, which is a Runnable, and I want to be able to give it the Activity (or obtain the Activity somehow), so it can call said method when it needs to.
This Runnable will eventually connect with a server, so it can update the application with information from the server. I've done client/server Java stuff before, so that's not the issue. I just need to figure out how to communicate between this Runnable and the Activity.
Originally, I was going to just pass the Activity itself in, but I read that it would create problems if I did. Instead, I was supposed to pass in an ApplicationContext via getApplicationContext(). I did that, but now I don't know what to do with the ApplicationContext. I tried casting it to the my Activity class, but the program just crashes.
How do I accomplish what I'm aiming at?
There are a few specific ways in Android to handle threading like AsyncTasks etc., you should read up on how to do 'painless' threading here. If it's just a one-off task where you connect to the server, get the value, set it in the TextView and then finish, I think an AsyncTask would be your best option. Continuing background processes are more suited to being services.
you can pass your activity to the constructor of your second Class like this :
public SecondClass(YourActivity _yourActivity){
this.activity = _yourActivity;
//do stuff
}
and in your Activity , you can instanciate your class like this :
SecondClass instance = new SecondClass(this);
NOTE : in your SecondClass , if you want to change the UI of your application , you can use the method runOnUiThread(Runnable);

Categories

Resources