有没有办法获取所有打开的活动的实例?实例,不是活动的名称?

时间:2018-08-16 13:12:16

标签: android android-activity

我要获取当前打开的活动的实例吗?是否有任何方法可以通过ActivityManager获取所有活动的实例。我认为ActivityManager仅给出活动的名称。

1 个答案:

答案 0 :(得分:0)

否!无法执行此操作。

有一种方法可以获取当前活动而不是实例的名称,因为您可能不拥有该活动。

相反,您可以保留对活动的引用,此示例显示了管理单个引用的方法,但是您可以设置进行所有实时活动的逻辑。

public class PagingTest {
    public static void main(String[] args) throws Exception {
        Hashtable<String, String> env = new Hashtable<>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.SECURITY_PRINCIPAL, "username");
        env.put(Context.SECURITY_CREDENTIALS, "password");
        env.put(Context.PROVIDER_URL, "ldap://campus.uni.ac.uk/DC=campus,DC=uni,DC=ac,DC=uk");
        LdapContext adContext = new InitialLdapContext(env, null);

        // Set up search controls.
        SearchControls searchControl = new SearchControls();
        searchControl.setSearchScope(SearchControls.SUBTREE_SCOPE);
        String[] attributesToFetch = {"cn"};
        searchControl.setReturningAttributes(attributesToFetch);

        // Set up a paged search.
        final int pageSize = 500;
        byte[] cookie = null;
        adContext.setRequestControls(new Control[]{
                new PagedResultsControl(pageSize, Control.CRITICAL)
                });

        // Do the search.
        int count = 0;
        boolean finished = false;
        while (!finished) {
            NamingEnumeration<SearchResult> records
                    = adContext.search("OU=Groups", "objectClass=group", searchControl);

            // Examine the page's results control response and act accordingly.
            Control[] controls = adContext.getResponseControls();
            if (controls != null) {
                for (int i = 0; i < controls.length; ++i) {
                    if (controls[i] instanceof PagedResultsResponseControl) {
                        PagedResultsResponseControl prrc =
                                (PagedResultsResponseControl) controls[i];
                        cookie = prrc.getCookie();
                        if (cookie == null) {
                            finished = true;
                        }
                    }
                }
            } else {
                cookie = null;
                finished = true;
            }

            // Process the page of results.
            while (records != null && records.hasMore()) {
                SearchResult sr = records.next();
                Attributes attribs = sr.getAttributes();
                BasicAttribute ba = (BasicAttribute) attribs.get("cn");
                String cn = (String) ba.get();
                System.out.println(cn);
                ++count;
            }

            // Re-activate paged results with the new cookie.
            adContext.setRequestControls(new Control[]{
                    new PagedResultsControl(pageSize, cookie, Control.CRITICAL)
                    });
        }
        System.out.println("Found " + count + " groups");
    }
}

您的应用程序类:

<application
    android:name=".MyApp"
    ....
 </application>

创建一个新的活动:

  public class MyApp extends Application {
        public void onCreate() {
              super.onCreate();
        }

        private Activity mCurrentActivity = null;
        public Activity getCurrentActivity(){
              return mCurrentActivity;
        }
        public void setCurrentActivity(Activity mCurrentActivity){
              this.mCurrentActivity = mCurrentActivity;
        }
  }

获取实时活动

public class MyBaseActivity extends Activity {
    protected MyApp mMyApp;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mMyApp = (MyApp)this.getApplicationContext();
    }
    protected void onResume() {
        super.onResume();
        mMyApp.setCurrentActivity(this);
    }
    protected void onPause() {
        clearReferences();
        super.onPause();
    }
    protected void onDestroy() {        
        clearReferences();
        super.onDestroy();
    }

    private void clearReferences(){
        Activity currActivity = mMyApp.getCurrentActivity();
        if (this.equals(currActivity))
            mMyApp.setCurrentActivity(null);
    }
}

Code ref

现在更改所有直播活动的代码。