该方法getSystemService(字符串)是未定义的类型听 [英] The method getSystemService(String) is undefined for the type Listen

查看:750
本文介绍了该方法getSystemService(字符串)是未定义的类型听的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查是否com.android.music运行,我发现了以下错误在这行 this.getSystemService(Context.ACTIVITY_SERVICE);

I'm trying to check if com.android.music is running and I'm getting the following error on this line this.getSystemService(Context.ACTIVITY_SERVICE);

错误:

The method getSystemService(String) is undefined for the type Listen

code:

Code:

public boolean isMusicRunning() {

            ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
            for (int i = 0; i < procInfos.size(); i++) {
                if (procInfos.get(i).processName.equals("com.android.music")) {
                    Toast.makeText(null, "music is running",
                            Toast.LENGTH_LONG).show();
                }
            }
        } 

如果你可以让我知道我在做什么错在这里,这将是伟大的!

If you could let me know what I'm doing wrong here that would be great!

感谢

推荐答案

getSystemService 是一个类的方法上下文,所以你需要在上下文中运行它。

getSystemService is a method of the class Context, so you'll need to run it on a context.

你可能是指其复制原来的code从一个活动派生类中运行。你需要通过一个上下文参数到你的方法,如果它不是一个活动里面。

The original code you copied it from was probably meant to be run from an Activity-derived class. You need to pass a Context argument into your method if it's not inside an Activity.

这篇关于该方法getSystemService(字符串)是未定义的类型听的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆