android获取当前运行Activity名字的方法

2020-01-26 18:04:22

本文实例讲述了android获取当前运行Activity名字的方法,可以避免即时聊天再出现通知的情况。分享给大家供大家参考。具体方法如下:


最近在做IM时需要知道当前Activity是哪一个Activity.自己整理一下两种方法


第一种:要方便一点(Service中无法使用)


private String getRunningActivityName() { 

        String contextString = context.toString(); 

        return contextString.substring(contextString.lastIndexOf(".") + 1, contextString.indexOf("@")); 

}


第二种:要稍微麻烦一点,需要一个权限(推荐)


<uses-permission android:name="android.permission.GET_TASKS" /> 


private String getRunningActivityName(){         

        ActivityManager activityManager=(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 

        String runningActivity=activityManager.getRunningTasks(1).get(0).topActivity.getClassName(); 

        return runningActivity;                

}





  • 2017-11-10 00:06:15

    CORS: credentials mode is 'include'

    XMLHttpRequest cannot load http://localhost/Foo.API/token. The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'http://localhost:5000' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

  • 2017-11-19 00:17:51

    Java如何获取Date的“昨天”与“明天”示例代码

    最近在做项目的时候用到Date和Calendar比较多,而且用到的方式也比较全,突然想到一个问题,Java如何获取Date的"昨天"与"明天",也就是前一天和后一天呢?思考后写出了方法,想着万一以后用到,就总结出来,也方便有需要的朋友们参考借鉴,下面来一起看看吧。

  • 2017-11-23 02:00:51

    js 分页插件twbs-pagination

    ​cdn地址 http://www.bootcdn.cn/twbs-pagination/ 官网地址 可以在cdn地址上面查看到