android 判断APP是否打开的方法

我们可能在实现notification的逻辑时,需要判断应用是在打开还是后台的状态,如果在后台状态,就发送Notification提醒。


/**
 * 判断程序是否打开
 * @return
 */
public static boolean isRunningInForeground() {
    boolean isActivityFound = false;

    ActivityManager activityManager = (ActivityManager) MyApplication.getContext().getSystemService(Context.ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> services = activityManager
            .getRunningTasks(1);

    if (services.get(0).topActivity.getPackageName().toString()
            .equalsIgnoreCase(MyApplication.getContext().getPackageName().toString())) {
        isActivityFound = true;
    }
    return isActivityFound;
}

<code style="margin: 0px; padding: 0px; border: 0px; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, sans-serif; white-space: inherit;"><span class="tag" style="margin: 0px; padding: 0px; border: 0px; color: rgb(125, 39, 39);"><uses-permission</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);"> </span><span class="atn" style="margin: 0px; padding: 0px; border: 0px; color: rgb(230, 67, 32);">android:name</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; color: rgb(48, 51, 54);">=</span><span class="atv" style="margin: 0px; padding: 0px; border: 0px; color: rgb(15, 116, 189);">"android.permission.GET_TASKS"</span><span class="tag" style="margin: 0px; padding: 0px; border: 0px; color: rgb(125, 39, 39);">/></span></code>

如果想判断别的应用,修改查询的app名

 
    if (services.get(0).topActivity.getPackageName().toString()
            .equalsIgnoreCase(查询的app包名)) {
        isActivityFound = true;
    }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值