how to manage button views from BroadcastReceiver - android

In MainActivity I have few buttons like start,stop and runInBackground..
I am using a service to send location updates..If the phone is restarted then my app will start automatically,I've achieved this by broadcastreceiver.
Now,I need to disable the start button of my app after restarting phone as because service is already started.
Leave the part of checking whether service is running or not,I am checking that using fileInputStream..That is another task..
But,How do I manage the Views which belongs to MainActivity class from a receiver class.
I've tried few things like this::
In MainActivity.java
public static void disable()
{
btnShowLocation.setEnabled(false);
btnStopLocation.setEnabled(true);
runbackground.setEnabled(true);
}
MyReceiver.class
public class MyReceiver extends BroadcastReceiver
{
MainActivity.disable()
}
it was not working..tried another like this:
MainActivity.btnShowLocation.setEnabled(false);
MainActivity.btnStopLocation.setEnabled(true);
MainActivity.runbackground.setEnabled(true);
even this crashing my app..
Now how do I manage button views from BroadcastReceiver..

You could:
Save the state in SharedPreferences (or similar) and query it on Activity's onCreate.
Fire an explicit intent from your receiver to start the Activity and put some extra data into that intent to tell the Activity to disable the button.
Somehow register your Activity with an Application class or a singleton and then call methods on it.

Related

reset TextView from two activities in OnReceive method

I have two acitivties and want to reset values in them everyday at 00:00.
Question:
Is there any way to link two activities to onReceive method of broadcastReceiver so I can update activities(TextView and EditText) from onReceive?
Create a BroadcastReceiver class and register it in your manifest. Also pass the instances of your activities to the BroadcastReceiver when you declare it so you can know which one you are clearing/using.
Now make your clearing methods public in both of your activities and call them from your BroadcastReceiver using the instances of each activity.
Check THIS tutorial and THIS one on how to use BroadcastReceiver class.

How do you call a method in an activity from a broadcastreceiver that is registered in manifest without starting the activity with an intent?

I have a extended broadcastreceiver class that listens for bluetooth connection/disconnection. I want it to change a color of some text in my GUIActivity. I don't have it as an inner class on purpose: to keep the GUI code more manageable/modular.
I know of one way to do this: register the receiver dynamically and pass it in the context of the activity. Then do the normal registering/unregistering in onResume and onPause. This solution can be seen at this post
However, I was hoping I could eliminate just a little bit more code by having my receiver registered in the manifest and not worry about registering/unregistering.
I have tried casting the context in the onReceive on the Broadcastreceiver as follows
((SmokinoGUI) context).indicateBTConnection();
This throws an exception saying that context cannot be cast to SmokinoGUI. indicateBTConnection() is a method in the SmoinoGUI activity that does what it says.
So, is there a way to call a method in an activity from a broadcastreceiver that has been registered in the manifest and has not been dynamically instantiated?
So, is there a way to call a method in an activity from a broadcastreceiver that has been registered in the manifest and has not been dynamically instantiated?
There is lot of ways. My favorite is:
Extends Application class and assign that class to your Application name attribute in Manifest.
Add instance of your Activity in that class and create getter and setter to it, that you could reference to in Activity onCreate() .
Get application in your BroadcasetReceiver and call the getter to the Activity.
If all this sound complicated, well, it really not, and it good logic to use in every app. I could add some code for example.

how to access Activity's instance variable from Service

I have an activity such that when the user onPause() on that activity, i start a pending intent that modifies some variable such as mSomeIntegerThatNeedsToBeReset after some time (using Alarm Manager and pending intents).
I tried to make this variable static, which works, but i don't want to make things static if i can help it, and i need to reset that value (in activity) from somewhere else.
public class SomeActivity extends Activity {
private Integer mSomeIntegerThatNeedsToBeReset;
}
what's the standard way to access this variable?
Sounds like you either need to bind your service to your activity OR
You could dispatch an Intent from your service to your Activity and clear the state variable.

Need Suggestion to do a task when Android Boots-Up

I have written an activity A, when users press a button, it will do MyConfig.doSomething() where MyConfig is simple class with activity A passed to it.
public class A extends PreferenceActivity {
private MyConfig mMyConfig;
/* pseudo code, when button clicked, call */
mMyConfig.doSomething();
}
In mMyConfig, it accesses SharedPreferences for some configuration. Thus, I can do this to pass the activity to mMyConfig for calling getSharedPreferences().
mMyConfig = new MyConfig ( this );
Here comes my request:
I want to do something that MyConfig.doSomething() already does, but except when users click some button to invoke it, I want to invoke it when Android Boots-Up.
I can write another class to extend BroadcastReceiver and then starts activity A by calling startActivity(A.class), and then in A, do some tricks to make mMyConfig.doSomething() happen. It works but the Application will be shown on screen when Android Boots-Up.
I want to make mMyConfig.doSomething() happen implicitly without letting users be aware of it. I suppose two possible solutions but I don't know how to do it.
Solution A:
Write a class that extends BroadcastReceiver, start a service (instead of activity A) that reads the SharedPreferences of A and create MyConfig object to do doSomething(). However, I don't if this can work if activity itself is never launched and how could I do this (read SharedPreferences from a service)?
Solution B:
Write a class that extends BroadcastReceiver, start activity A without showing it, put it to activity stack by calling startActivity(A.class) in onReceive. Is this possible?
Instead of Activity, which are meant to be visible to the user, you can make your BoardcastReceiver to start a Service instead. It is meant to perform tasks in the background without disturbing the user. The official guide is a nice place to start with.
Edited:
To access the SharedPreference of your application, simply call this line inside your service:
SharedPreferences pref = PreferenceManager.getSharedPreferences();

How to find reference of an Activity from a library and close(finish()) the activity?

Scenario :
An activity is displayed. It has a text box and a button. If you enter a special code in the textbox and click the button the activity closes after comparing the text logic. Simple! This has been implemented in the OnCLick() of the the activity.
Problem :
I have a library that handles all the the SMS receiver functionalities. The receiver remotely listens for a special incoming SMS. Once the special sms arrives, a library function (closeActivity) should either
finish() the activity(described above) straightaway
imitate the action of entering text + clicking button by getting a reference to the activity somehow?
I am fine with either. which ever is simpler and safer!
I just need to know how/where/what code to write in the library so that it can finish() the activity!
Assumption: The activity is always displayed and the system is not stacking it automatically!
Why dont you try to use intents? esp broadcast intents.. Fire the broadcast intent from library and get it in ur activity /another class registered as intent reciever . You can get the instance of activity in the broadcast reciever easily.
public static <Your activity class> activitySingleTon
and set the singleton in appropriate lifecycle method.
OnCreate(){ activitySingleTon = this; }
and use in broadcast reciever like this
if(<your activity class>.activitySingleTon)
{ <your activity class>.activitySingleTon.finish(); }
Can there be several instances of the Activity?
If not, make it a singleton, then reference it from anywhere with MyActivity.getInstance().
Best regards.

Categories

Resources