I know the BroadcastReceiver can't be used if defined as Activity's inner class. But I wonder why? Is it because the system would have to instantiate a large Activity object to just have instantiated a receiver instance?
... because the system would have to instantiate a large Activity object to just have instanitated a recevier instance?
Yup, just like any other non-static inner class. It has to get an instance of the outer class from somewhere (e.g. by instantiating or by some other mechanism) before it can create an instances of the (non-static) inner class.
Global broadcast receivers that are invoked from intents in the manifest file that would be be instantiated automatically by the system have no such outer instance to use to create an instance of the broadcast receiver non-static inner class. This is independent of what the outer class is, Activity or not.
However, if you are using a receiver as part of working with an activity, you can manually instantiate a broadcast receiver yourself in the activity (while one of the activity callbacks, you have an instance of the outer class to work with: this) and then register/unregister it as appropriate:
public class MyActivity extends Activity {
private BroadcastReceiver myBroadcastReceiver =
new BroadcastReceiver() {
#Override
public void onReceive(...) {
...
}
});
...
public void onResume() {
super.onResume();
....
registerReceiver(myBroadcastReceiver, intentFilter);
}
public void onPause() {
super.onPause();
...
unregisterReceiver(myBroadcastReceiver);
}
...
}
Related
I have an Activity:
public class MyActivity extends Activity {
#Override
protected void onDestroy() {
// TODO: send event to other class
}
}
and a separate class:
public class MyClass{
MyClass(Context context){
// I have the context of activity
}
// This needs to be called by MyActivity in all other instantiates
void onActivityDestroy(){
}
}
MyClass is instantiated in other places and I want onActivityDestroy to be called in those instantiates. The place where MyClass is instantiated is not accessible.
I am looking for a solution that uses interfaces, listener, messages... but not static fields.
You can maintain the list of MyClass instances at application level then access that list in OnDestroy method of activity. And execute onActivityDestroy() version of each instance.
You should maintain list of instances in your Application class, whenever MyClass instance is created, you push that instance to the list maintained at Application Class
// this is to push the MyClass instance.
Context.getApplication().getMyClassInstanceList().add(new MyClass());
public class MyActivity extends Activity {
#Override
protected void onDestroy() {
List<Myclass> myClassObjects = Context.getApplication.getMyClassInstaceList();
for(Myclass myclass : myClassObjects)
{
myclass.onActivityDestroy();
}
}
}
Is there a way to call a non-static method of an activity from a wearableListenerService?
(Binding the service is not possible for wearableListenerService. Because the method onBind() is final.)
Simplified example:
Service.class:
public class MyService extends WearableListenerService {
#Override
public void onMessageReceived(MessageEvent messageEvent){
myActivityMethod();
}
...
}
Activity.class:
public class MyActivity extends Activity {
public void myActivityMethod(){
//do sth.
}
...
}
you could Broadcast an event and register a BroadcastReceiver in your Activity. You could also connect the GooglePlayClient in your Activity and let it handle the onMessageReceived event, if the current activity is the current at screen
There are 2 Fragments
I'm calling a service from Fragment 1. I have a ResultReceiver in Fragment 1 which listens to the result and onReceiveResult will call method1().
I want a ResultReceiver in Fragment 2 to listen to the same response but onReceiveResult will be calling method2()
How can I achieve this?
You could specify an interface:
interface Receiver {
void onResult();
}
Have your two Fragments implement this interface. Fragment1's implementation simply calls method1(), and Fragment2's implementation simply calls method2():
public class Fragment1 extends Fragment implements Receiver {
// Remember to register and remove the receiver (e.g. in onAttach and onDetach respectively).
private MyReceiver mBroadcast = new MyReceiver(this);
public void onResult() {
this.method1();
}
}
public class Fragment2 extends Fragment implements Receiver {
// Remember to register and remove the receiver (e.g. in onAttach and onDetach respectively).
private MyReceiver mBroadcast = new MyReceiver(this);
public void onResult() {
this.method2();
}
}
Then specify the BroadcastReceiver as a standalone (or inner static) class such that both Fragment1 and Fragment2 will be able to instantiate it:
public class MyReceiver extends BroadcastReceiver {
private Receiver mFragment;
public MyReceiver(Receiver fragment) {
mFragment = fragment;
}
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals(YOUR_ACTION) {
mFragment.onResult();
}
}
}
I don't think that you can receive results in two different fragments simultaneously.
But there are many ways to achieve this..
1.
I believe the easiest way will be to use object reference..
There are two possibilities.. Either create a static method in Fragment 2 and call it from fragment 1 from onReceiveResult(). Or Create an object of Fragment 2 in fragment 1 and from fragment 2 , assign that it is the same as the instance created by fragment1. Then just call
object_of_frgament2.method2() from the onReceiveResult() of fragment 1.
2.
Using interface.Create a custom interface and make the Fragment 2 implement the interface and create an instance of the interface in Fragment 1.
and within onReceiveResult() of Fragment1 you can call the interface method.
While implementing the interface, you can get the result in fragment 2 in the interface method.
Just call method2() from the function....
3.Using Broadcast Receiver..
Create a custom broadcast receiver and make all the fragments/activities which need the results to listen to it. and within onReceiveResult() of Fragment1 just broadcast the result..
I believe there are still other ways to do it..
just pass into your service two different ResultReceiver's ... If the service is already started calling startService(Intent) again just makes you call onStartCommand(...) and then you can set your resultReciever each time. So you can keep an array of resultreciever's if you want.
saying that, i would never do it this way. Research Java Observer pattern. Java has a default implementation of the Observer pattern. Here is a link
First question:
How can we use
ActivityManager activity =(ActivityManager)getSystemService(ForegroundApp.ACTIVITY_SERVICE)
And also
getPackageManager in a class that extends Thread?? I'm trying to fetch the Foreground activity's package name, its start time and end time in the foreground. I know this is Context based but I need to run this is in a thread.
Second Question:
How can we call an abstract class that extends thread in a service??
This thread only runs when the screen is ON. So, I'll be registering for Screen ON and OFF intents in a service. In this service, when the screen is on, i need to call this Thread.
Example:
abstract class A extends Thread {
abstract method met();
public void run() {
//Find out foreground's app name and its start and end time.
}
}
class B extends service {
//Here i need to call A.
}
All what you have to do is simply pass an reference to your Activity in first case to class A, and reference to class A to class B. Easiest way is to pass by constructor.
abstract class A extends Thread {
protected Activity activity;
public A(Activity activity) {
this.activity = activity;
}
abstract method met();
public void run() {
// activity.yourmethods()
//Find out foreground's app name and its start and end time.
}
}
I have a service which has a method that downloads an image from an URL and returns an Uri.
That service will get more complex when it has all the intended features. Therefore,
I'm invoking its methods within a thread.
My problem is how to warn the activity that the service has done it's work.
I could change a class isFinished variable but the activity had to be constantly checking
for its value.
I just want the service to tell the activity that it's work is done and the resources are
available for use.
I thought something in the lines of the service calling stopSelf() and the activity was
warned through "onServiceDisconnected" but that didn't seem very "political correct".
Thanks in advance
There are two ways to do it.
1. You can start your activity using by firing an intent.
2. You can Broadcast an intent and write receiver for it in your app when your receiver receives intent and onreceive method is called in this method you can start your activity using intent.
cheers...
public class MyActivity extends Activity{
public MyActivity() {
...
MyThread thread = new MyThread(this);
thread.start();
}
public void onFinishedThread(...) {
}
}
class MyThread extends Thread {
MyActivity activity;
public MyThread(MyActivity activity) {
this.activity = activity;
}
public void run() {
// do work
...
this.activity.onFinishedThread(...);
}
}