Android: Want to call main activity's class function in service class - android

Here I have a main class that extends activity and having a function say abc(). Now I want to call it in my services class. But I am unable to do that plz some one help as I found error : "Source not found"..

Maybe some of the related Topics/Questions have already solved your problem, e.g.
In Android: How to Call Function of Activity from a Service?
Call a Method in the main activity from a custom view
How to call An activity class from a service class

Related

How to call to a method that is in the MainActivity from another class

I want to call a method that is in my MainActivity class and I don't know how to do it if I want to call it in another class. How can I do it?
you can call it using the main activity context this might help : Calling method of another class using contexts

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 start activity from non-activity class,if first activity is simple java class?

I want to start activity from 1st java class that extends BroadcastReceicer class.so i can`t extends activity class to start activity of another java class by using startactivity method in 1st class.
Give me proper guidance what should i do now?
use context in the Onrecieve function of the broadcastreciever,
context.startActivity(...);

Can't extend main class with anything but Activity

Hi
I am trying to extend my main entry point with a superclass (so I don't have to override onCreateOptionsMenu in each activity i got for example) but for some reason it seems I am not allowed to do this with the class I got set as my main entry point for my application in android.
For example: MainClass extends Activity works fine but MainClass extends mySuperClass where mySuperClass extends Activity does not work - anyone got any idea if I am doing something wrong or if this is an android specific issue?
Thanks for any help or input!
All Android Activity classes MUST extend an Activity base class. Your superclass definition must be missing something: post the class definition.

How to call method of another class in android

I have two different class(class A and Class B). i want to use the method of Class A in Class B. i normally used object for class A and called method in class B. but unfortunately i am getting Force close error. Is that any thing different to call a method of another class in android. I referred many articles in stackoverflow. but i cant understand properly. pls help me to find out the solution.
You should not create object like this, you should use context to call object like as below
((Class A) contextObject).function();
it runs perfectly on my system,
earlier class A and class B both are extending Activity and now only A extends Activity and B extends A and now B can call functions of A
this works for me:-
public class A extends Activity
{
functionOfA(){}
}
public class B extends A
{
//calling function of class A
functionOfA();
}
In case of Android, class which extends Activity will maintain its life cycle methods. if method which is defined in different class other current running activity may be killed or in pause state. so it is suggested that if method which is reusable in application should in different class for example (AppManager singleton class) rather than being in single activity class
u have to create constructor of class A
& in class B make an obj to class A
initialize it with
ClassA obj=new classA();
obj.method_A();
hope this will help
As I said in my comment, you shouldnt instantiate activities.
If your method uses some method that are called from a Context object, you can create a new class ( Class NewClass) that accepts a context parameter and implements your methods in it.
So this way, you can call your class from any activity:
NewClass nc = new NewClass(this);
Look up for some example of how to use a database in Android. It uses the same way.

Categories

Resources