简体   繁体   English

日志/logcat 在 BroadcastReceiver 中不起作用

[英]Logs/logcat not working in BroadcastReceiver

I have a BroadcastReceiver and it there is some problem.我有一个 BroadcastReceiver,它有一些问题。 The Toast message is showing, and the logcat message is appearing with the command line logcat tool, but not in the Android studio Logcat display. Toast 消息正在显示,并且 logcat 消息与命令行 logcat 工具一起出现,但不在 Android studio Logcat 显示中。 Why?为什么? I have already tried android:debuggable="true" and anything has not changed.我已经尝试过android:debuggable="true"并且没有任何改变。

public class AlarmReceiver extends BroadcastReceiver {
        private String filePath;
        private Ringtone ringtone;

        @Override
        public void onReceive(Context context, Intent intent) {
            Log.d("RECEIVE", "");
            Bundle extras = intent.getExtras();
            if (extras == null) {
                filePath = null;
            } else {
                filePath= extras.getString("filePath");
            }
            Log.d("Filepath in receiver", filePath);
            ringtone = RingtoneManager.getRingtone(context, Uri.parse(filePath));
            ringtone.play();
            Toast.makeText(context, "Fooo", Toast.LENGTH_LONG).show();
            //   setRingsong(context, filePath);
        }
    }

Try this on a shell.在 shell 上试​​试这个。 adb logcat D |grep RECEIVE adb program can be found on your sdk sdk tools(platform-tools) this is the program the actual debugger use when retreiving the logs. adb logcat D |grep RECEIVE adb 程序可以在您的 sdk sdk 工具(平台工具)中找到,这是在检索日志时实际调试器使用的程序。

The "D" parameter indicates that it will show "debug" logs, this complements the android:debuggable="true" setting if you wish to print logs while debugging (remember these are two different things, printing logs with the Log object and setting "android.debuggable = true",check this link for more information about debugging with AndroidStudio). “D”参数表示它将显示“调试”日志,如果您希望在调试时打印日志,这补充了 android:debuggable="true" 设置(记住这是两个不同的事情,使用 Log 对象打印日志和设置“android.debuggable = true”,请查看此链接以获取有关使用 AndroidStudio 进行调试的更多信息)。 Otherwise, try using Log.e function instead of Log.d.否则,请尝试使用 Log.e 函数而不是 Log.d。 Log.e is usually for errors, in this case for testing purposes you can use it and ensure your logs will always be displayed, due to error logs having a higher priority thatn debug logs. Log.e 通常用于错误,在这种情况下,出于测试目的,您可以使用它并确保始终显示您的日志,因为错误日志的优先级高于调试日志。

  1. ( optional step ) At first create a static/const variable named TAG: optional step )首先创建一个名为 TAG 的静态/常量变量:

     const val TAG = "aaa"
  2. In Logcat window -> Filters DropDown -> Edit Filter Configuration:在 Logcat 窗口 -> 过滤器下拉 -> 编辑过滤器配置:

在此处输入图片说明

  1. Then in Log Tag: field enter the name chosen in the first step:然后在Log Tag:字段中输入在第一步中选择的名称:

2

You can now log what you want in BroadcastReceiver or anywhere else in your project:您现在可以在 BroadcastReceiver 或项目中的任何其他地方记录您想要的内容:

class BroadcastReceiver : BroadcastReceiver() {

  override fun onReceive(context: Context?, intent: Intent?) {
      context ?: return

      Log.d(TAG, intent?.action!!)
  }
}

and the result will be like:结果将是这样的:

3

The problem is that the tag should not be all uppercase.问题是标签不应该全部大写。 If you use "receive" or "Receive", it will be fine.如果您使用“接收”或“接收”,就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM