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

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

问题描述

我有一个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. (可选步骤),首先创建一个名为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天全站免登陆