use*_*571 141 android android-activity
有没有简单的方法来确定某项活动是否有效?我想根据哪些活动是活跃的来做某些事情.例如:
if(activityrunning == activity1)
//do this
else if (activityrunning == activity2)
//do something else
Run Code Online (Sandbox Code Playgroud)
sil*_*gle 213
您可以static在活动中使用变量.
class MyActivity extends Activity {
static boolean active = false;
@Override
public void onStart() {
super.onStart();
active = true;
}
@Override
public void onStop() {
super.onStop();
active = false;
}
}
Run Code Online (Sandbox Code Playgroud)
唯一的问题是如果你在两个相互链接的活动中使用它,那么onStop在第一个活动中有时会onStart在第二个活动中调用它.所以两者都可能是短暂的.
取决于您要执行的操作(从服务更新当前活动?).您可以在activity onStart方法中在服务中注册一个静态侦听器,然后在您的服务想要更新UI时,可以使用正确的侦听器.
Xen*_*one 41
我觉得更清楚:
public boolean isRunning(Context ctx) {
ActivityManager activityManager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (RunningTaskInfo task : tasks) {
if (ctx.getPackageName().equalsIgnoreCase(task.baseActivity.getPackageName()))
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
GaR*_*eTa 26
不使用任何辅助变量的选项是:
activity.getWindow().getDecorView().getRootView().isShown()
Run Code Online (Sandbox Code Playgroud)
活动是fe:this或getActivity().
此表达式返回的值在onStart()/ onStop()中更改,这些事件是启动/停止显示手机上活动布局的事件.
小智 23
我使用了MyActivity.class和getCanonicalName方法,我得到了答案.
protected Boolean isActivityRunning(Class activityClass)
{
ActivityManager activityManager = (ActivityManager) getBaseContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (ActivityManager.RunningTaskInfo task : tasks) {
if (activityClass.getCanonicalName().equalsIgnoreCase(task.baseActivity.getClassName()))
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
Xar*_*mer 20
比使用静态变量和遵循OOP更好的方法
Shared Preferences可用于与其他activities人和服务共享变量application
public class example extends Activity {
@Override
protected void onStart() {
super.onStart();
// Store our shared preference
SharedPreferences sp = getSharedPreferences("OURINFO", MODE_PRIVATE);
Editor ed = sp.edit();
ed.putBoolean("active", true);
ed.commit();
}
@Override
protected void onStop() {
super.onStop();
// Store our shared preference
SharedPreferences sp = getSharedPreferences("OURINFO", MODE_PRIVATE);
Editor ed = sp.edit();
ed.putBoolean("active", false);
ed.commit();
}
}
Run Code Online (Sandbox Code Playgroud)
使用共享首选项.它具有最可靠的状态信息,较少的应用程序切换/销毁问题,节省了我们请求另一个权限,它让我们有更多的控制权来决定我们的活动何时实际上是最顶层的.这里也看到 abd的详细信息
这是用于检查特定服务是否正在运行的代码.只要您使用getRunningAppProcesses()或getRunningTasks()更改getRunningServices,我就相当确定它可以用于活动.看看这里http://developer.android.com/reference/android/app/ActivityManager.html#getRunningAppProcesses()
相应地更改Constants.PACKAGE和Constants.BACKGROUND_SERVICE_CLASS
public static boolean isServiceRunning(Context context) {
Log.i(TAG, "Checking if service is running");
ActivityManager activityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);
boolean isServiceFound = false;
for (int i = 0; i < services.size(); i++) {
if (Constants.PACKAGE.equals(services.get(i).service.getPackageName())){
if (Constants.BACKGROUND_SERVICE_CLASS.equals(services.get(i).service.getClassName())){
isServiceFound = true;
}
}
}
Log.i(TAG, "Service was" + (isServiceFound ? "" : " not") + " running");
return isServiceFound;
}
Run Code Online (Sandbox Code Playgroud)
if(!activity.isFinishing() && !activity.isDestroyed())
Run Code Online (Sandbox Code Playgroud)
来自官方文档:
检查此活动是否正在完成,因为您对其调用了 finish() 或其他人要求它完成。这通常用于 onPause() 来确定活动是简单地暂停还是完全结束。
如果对 Activity 进行了最后的 onDestroy() 调用,则返回 true,因此该实例现在已死亡。
我意识到这个问题已经很久了,但是我认为仍然值得分享我的解决方案,因为它可能对其他人有用。
在发布Android体系结构组件之前,该解决方案不可用。
活动至少部分可见
getLifecycle().getCurrentState().isAtLeast(STARTED)
Run Code Online (Sandbox Code Playgroud)
活动处于前台
getLifecycle().getCurrentState().isAtLeast(RESUMED)
Run Code Online (Sandbox Code Playgroud)
谢谢kkudi!我能够使你的答案适应一项活动......这就是我的应用程序中的工作原理..
public boolean isServiceRunning() {
ActivityManager activityManager = (ActivityManager)Monitor.this.getSystemService (Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE);
isServiceFound = false;
for (int i = 0; i < services.size(); i++) {
if (services.get(i).topActivity.toString().equalsIgnoreCase("ComponentInfo{com.lyo.AutoMessage/com.lyo.AutoMessage.TextLogList}")) {
isServiceFound = true;
}
}
return isServiceFound;
}
Run Code Online (Sandbox Code Playgroud)
如果topActivity与用户正在进行的操作匹配,此示例将为您提供true或false.因此,如果您正在检查的活动没有显示(即是onPause),那么您将无法获得匹配.此外,要执行此操作,您需要向清单添加权限.
<uses-permission android:name="android.permission.GET_TASKS"/>
Run Code Online (Sandbox Code Playgroud)
我希望这可以帮到你!
有比上述所有方法更简单的方法,这种方法不需要android.permission.GET_TASKS在清单中使用,也不需要在接受的答案中指出竞争条件或内存泄漏的问题。
在主活动中创建一个 STATIC 变量。静态允许其他活动从另一个活动接收数据。onPause()将此变量设置为false,onResume并将onCreate()此变量设置为true。
private static boolean mainActivityIsOpen;
Run Code Online (Sandbox Code Playgroud)分配此变量的 getter 和 setter。
public static boolean mainActivityIsOpen() {
return mainActivityIsOpen;
}
public static void mainActivityIsOpen(boolean mainActivityIsOpen) {
DayView.mainActivityIsOpen = mainActivityIsOpen;
}
Run Code Online (Sandbox Code Playgroud)然后从另一个活动或服务
if (MainActivity.mainActivityIsOpen() == false)
{
//do something
}
else if(MainActivity.mainActivityIsOpen() == true)
{//or just else. . . ( or else if, does't matter)
//do something
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
229470 次 |
| 最近记录: |