日志/logcat 在 BroadcastReceiver 中不起作用 [英] Logs/logcat not working in BroadcastReceiver

查看:31
本文介绍了日志/logcat 在 BroadcastReceiver 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个广播接收器,但它有一些问题.Toast 消息正在显示,并且 logcat 消息与命令行 logcat 工具一起出现,但不在 Android studio Logcat 显示中.为什么?我已经尝试过 android:debuggable="true" 并且没有任何改变.

I have a BroadcastReceiver and it there is some problem. 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. Why? I have already tried android:debuggable="true" and anything has not changed.

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);
        }
    }

推荐答案

  1. (optional step) 首先创建一个名为TAG的静态/常量变量:

  1. (optional step) At first create a static/const variable named TAG:

const val TAG = "aaa" 

  • 在 Logcat 窗口中 ->过滤器下拉菜单 ->编辑过滤器配置:

  • In Logcat window -> Filters DropDown -> Edit Filter Configuration:

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

    您现在可以在 BroadcastReceiver 或项目中的任何其他地方记录您想要的内容:

    You can now log what you want in BroadcastReceiver or anywhere else in your project:

    class BroadcastReceiver : BroadcastReceiver() {
    
      override fun onReceive(context: Context?, intent: Intent?) {
          context ?: return
    
          Log.d(TAG, intent?.action!!)
      }
    }
    

    结果会是这样的:

    这篇关于日志/logcat 在 BroadcastReceiver 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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