Reputation: 55
I have a function with an increment counter and I call it when the user click a button. I want to make a new button when user press it call this function every second. I used alarm manager and works fine.But when I try to call this function from broadcast receiver give me error because is not static.What can i do?
public class MyStartServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Send ....",Toast.LENGTH_LONG).show();
//Intent service = new Intent(context, AlarmService.class);
//context.startService(service);
//String msg = intent.getStringExtra("data");
//String msg="data";
a1class.function1();
}
public void function1(){
counter++;
//Toast.MakeText( counter );
}
Upvotes: 0
Views: 769
Reputation: 44571
Either make function1()
static
or, assuming a1class
is the class its in, create an instance of the class and call the function that way
a1class a1class = new a1class();
a1class.function1();
Upvotes: 2
Reputation: 717
If you want to call a non static method you need a instance of the class to call it on.
You can make a static method that returns the instance like this
private static ClassName instance;
public static ClassName getInstance(){
if (instance == null){
instance = new ClassName();
}
return instance;
}
You can also set the instance in the onCreate method.
Upvotes: 0