0

I wonder if there is a way to get the context of current running activity on android. For example, I have an Activity Class and it is running. What I want is to call another Simple Class to run some functions which called from Activity Class. By doing this, I need to set up a context of Activity Class on Simple Class; On the other way, I need to have the context of Current Running Activity, so that my Simple Class can actually run the functions called from Current Running Activity.

Below is the soft code from my project.

public class Main1 extends Activity {
    private static GetAPNsInfo getAPNsInfo = new GetAPNsInfo();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                getAPNsInfo.doSomething();
    }
}

public class GetAPNsInfo {
    public void doSomething() {
        Button button = currentRunningActivityContext.findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
            }
        });
    }
}

Finally, my purpose of this thread is that I need a good way to get the current running activity info.

This is a solution that I found my self. But it doesn't totally solve this case. We can add a receiver in Manifest.xml. This will run a background application.

<receiver android:name=".RunningActivityCapture">
        <intent-filter android:priority="-1">
            <action android:name="android.intent.action.NEW_OUTGOING_CALL"></action>
        </intent-filter>

    </receiver>

The background application interface look like this:

public class RunningActivityCapture extends BroadcastReceiver {

    @Override
    public void onReceive(Context aContext, Intent intent) {
        String action = intent.getAction();

        if (action.equals(Intent.ACTION_ALL_APPS)) {
            Activity activty = intent.getCurrentActivity();
            Session session = new Session();
            session.setRunningActivity(activty);
        }
        return;
    }
}

I only get the activity from session class which setting from my background application. This is my first idea to solve this problem. But the code is not correct. So I need your help.

skynet
  • 9,898
  • 5
  • 43
  • 52
user964409
  • 121
  • 3
  • 12

1 Answers1

3

If you are asking how to use this MainActivity in your helper class, you can pass the Activity itself into your class, as an activity is a Context. So your constructor will be:

public GetAPNsInfo(Context context) {
    ...
}

where you store the context in a field and use it later. You will initialize with

private static GetAPNsInfo getAPNsInfo = new GetAPNsInfo(this);

if you are asking how to get the context for ANY activity in your application, I don't think that is recommended.

skynet
  • 9,898
  • 5
  • 43
  • 52
  • It is what I'm not going to need. I already added a final purpose in my thread. What I want is getting current running activity info. If there are hundred tasks on hundred activities run the same thing, so do I need to set Context for each activity? It will become a huge work if doing this. – user964409 Oct 28 '11 at 03:25
  • Maybe it is not recommended to get ANY activity in my application. But there should be a way to get only one top running activity. May I use receiver to solve this? – user964409 Oct 28 '11 at 03:32
  • What are you trying to do this for? Perhaps there is a better way – skynet Oct 28 '11 at 04:09
  • Perhaps there is a good way for capture one top activity. I'm searching for ActivityManager and RunningTaskInfo. These should help. – user964409 Oct 28 '11 at 04:23
  • Most likely you will only cause more problems if you try to do this. For example, in your question you have code that uses `findViewById` to find a button in the currently running activity. However, what happens when that activity has no such button? – skynet Oct 28 '11 at 04:28
  • Yeah that is just a sample. But in reality, I shouldn't do this if my application doesn't have that button. By the way, we can get the id of a button in R.java even it doesn't exist in our layout, it only cause an error (component not found) when running the application (error doesn't appear on code). In other situation, I will get the context of current activity and then call getContentResolver on this activity. – user964409 Oct 28 '11 at 05:04
  • Maybe you should know the receiver. I will post a sample for this in the answer. – user964409 Oct 28 '11 at 05:07