每当我的广播执行时,我想显示对前景活动的警报。


当前回答

(注:在API 14中添加了官方API:见此答案https://stackoverflow.com/a/29786451/119733)

不要使用以前的(waqas716)答案。

由于对活动的静态引用,您将有内存泄漏问题。欲了解更多详情,请参阅以下链接http://android-developers.blogspot.fr/2009/01/avoiding-memory-leaks.html

为了避免这种情况,您应该管理活动引用。 在manifest文件中添加应用程序的名称:

<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);
    }
}

所以,现在不是为你的活动扩展Activity类,而是扩展MyBaseActivity。现在,你可以像这样从应用程序或活动上下文中获取当前活动:

Activity currentActivity = ((MyApp)context.getApplicationContext()).getCurrentActivity();

其他回答

我在@gezdy的答案上方展开。

在每个活动中,我们可以使用下面的API,而不是通过手动编码将自己“注册”到应用程序中,这是第14级以来的API,可以帮助我们用更少的手动编码实现类似的目的。

public void registerActivityLifecycleCallbacks (Application.ActivityLifecycleCallbacks callback)

http://developer.android.com/reference/android/app/Application.html#registerActivityLifecycleCallbacks%28android.app.Application.ActivityLifecycleCallbacks%29

在应用程序中。ActivityLifecycleCallbacks,您可以得到哪个活动“附加”到或“分离”到这个应用程序。

但是,这种技术只在API级别14之后才可用。

我找不到让我们团队满意的解决方案,所以我们就自己动手了。我们使用ActivityLifecycleCallbacks来跟踪当前活动,然后通过服务公开它。更多详情请访问:https://stackoverflow.com/a/38650587/10793

为了向后兼容:

ComponentName cn;
ActivityManager am = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
    cn = am.getAppTasks().get(0).getTaskInfo().topActivity;
} else {
    //noinspection deprecation
    cn = am.getRunningTasks(1).get(0).topActivity;
}

waqas716的答案很好。我针对需要更少代码和维护的特定情况创建了一个变通方案。

我通过一个静态方法从我怀疑是在前景的活动中获取一个视图,找到了一个特定的工作。 您可以遍历所有活动并检查您是否希望或从martin的回答中获得活动名称

ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity; 

然后我检查视图是否为空,并通过getContext()获取上下文。

View v = SuspectedActivity.get_view();

if(v != null)
{
    // an example for using this context for something not 
    // permissible in global application context. 
    v.getContext().startActivity(new Intent("rubberduck.com.activities.SomeOtherActivity"));
}

(注:在API 14中添加了官方API:见此答案https://stackoverflow.com/a/29786451/119733)

不要使用以前的(waqas716)答案。

由于对活动的静态引用,您将有内存泄漏问题。欲了解更多详情,请参阅以下链接http://android-developers.blogspot.fr/2009/01/avoiding-memory-leaks.html

为了避免这种情况,您应该管理活动引用。 在manifest文件中添加应用程序的名称:

<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);
    }
}

所以,现在不是为你的活动扩展Activity类,而是扩展MyBaseActivity。现在,你可以像这样从应用程序或活动上下文中获取当前活动:

Activity currentActivity = ((MyApp)context.getApplicationContext()).getCurrentActivity();