如何在android中检查应用程序是否打开

如何在android中检查应用程序是否打开,android,Android,我想当用户单击通知时,如果应用程序已打开,请打开Activity1。否则,如果应用程序已关闭并销毁,请打开Activity2。 我在我的服务中编写此方法(接收和创建通知): 但它不起作用,总是打开活动1。您能帮助我吗?继承下面的BaseActivity和应用程序中的所有活动 BaseActivity.kt abstract class BaseActivity : AppCompatActivity() { private val preferences: SharedPreferen

我想当用户单击通知时,如果应用程序已打开,请打开Activity1。否则,如果应用程序已关闭并销毁,请打开Activity2。 我在我的服务中编写此方法(接收和创建通知):


但它不起作用,总是打开活动1。您能帮助我吗?

继承下面的BaseActivity和应用程序中的所有活动

BaseActivity.kt

abstract class BaseActivity : AppCompatActivity() {
     private val preferences: SharedPreferences by lazy {
         PreferenceManager.getDefaultSharedPreferences(this)
     }

     override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          preferences.isApplicationRunning = true
     }

     override fun onDestroy() {
         preferences.isApplicationRunning = false
         super.onDestroy()
     }

}
SharedReferenceExt.kt

var SharedPreferences.isApplicationRunning: Boolean
set(value) {
    edit {
        putBoolean("APPLICATION_RUNNING", value)
    }
}
get() = getBoolean("APPLICATION_RUNNING", false)
your_service.kt

class YourService : Service() {

private val preferences: SharedPreferences by lazy {
    PreferenceManager.getDefaultSharedPreferences(this)
}

//below is the snippet of yours. changed the condition alone
private fun openActivity() {       
        if (preferences.isApplicationRunning) {
            notificationIntent = new Intent(getBaseContext(), Activity1.class);
        } else {
            notificationIntent = new Intent(getBaseContext(), Activity2.class);
            notificationIntent.putExtra("NotificationMessage", "I am from Notification");
            notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            notificationIntent.setAction(Intent.ACTION_MAIN);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        }

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder sNotifBuilder = new NotificationCompat.Builder(getBaseContext(), offerChannelId);
        sNotifBuilder.setContentIntent(pendingIntent);
       }
}

我希望你正在寻找你的应用程序是否在前台。如果是这种情况,您需要在应用程序类中注册
activitylifecycellbacks
,并跟踪
onActivityStarted
onActivityStopped
。如果您的任何活动处于启动状态,则表示您的应用程序处于前台

或者,如果你想知道在这种情况下,应用程序是否完全关闭,似乎没有确定的方法。不建议跟踪活动上的


在某些情况下,系统会简单地终止活动的宿主进程,而不在其中调用此方法(或任何其他方法),因此不应使用此方法来执行在进程结束后保留的操作,如文档中所述

当服务在后台运行时,
isAppRunning
将始终返回
true
。为了处理您的用例,我有一种不同的方法,我使用了SharedReference的思想来满足您的需求。因为我们是优先存储的,所以即使应用程序被终止,数据也会被持久化。我已保持应用程序的运行状态。请尝试一下,如果答案有效,请接受。这是个好主意,谢谢。但有一个问题。假设在程序打开时发送了通知,但用户没有单击,请关闭应用程序,然后单击通知。SharedReferences返回true,但应用程序已关闭!我的意思是清楚?
var SharedPreferences.isApplicationRunning: Boolean
set(value) {
    edit {
        putBoolean("APPLICATION_RUNNING", value)
    }
}
get() = getBoolean("APPLICATION_RUNNING", false)
class YourService : Service() {

private val preferences: SharedPreferences by lazy {
    PreferenceManager.getDefaultSharedPreferences(this)
}

//below is the snippet of yours. changed the condition alone
private fun openActivity() {       
        if (preferences.isApplicationRunning) {
            notificationIntent = new Intent(getBaseContext(), Activity1.class);
        } else {
            notificationIntent = new Intent(getBaseContext(), Activity2.class);
            notificationIntent.putExtra("NotificationMessage", "I am from Notification");
            notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            notificationIntent.setAction(Intent.ACTION_MAIN);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        }

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder sNotifBuilder = new NotificationCompat.Builder(getBaseContext(), offerChannelId);
        sNotifBuilder.setContentIntent(pendingIntent);
       }
}