[英]Logs/logcat not working in BroadcastReceiver
我有一个 BroadcastReceiver,它有一些问题。 Toast 消息正在显示,并且 logcat 消息与命令行 logcat 工具一起出现,但不在 Android studio Logcat 显示中。 为什么? 我已经尝试过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);
}
}
在 shell 上试试这个。 adb logcat D |grep RECEIVE adb 程序可以在您的 sdk sdk 工具(平台工具)中找到,这是在检索日志时实际调试器使用的程序。
“D”参数表示它将显示“调试”日志,如果您希望在调试时打印日志,这补充了 android:debuggable="true" 设置(记住这是两个不同的事情,使用 Log 对象打印日志和设置“android.debuggable = true”,请查看此链接以获取有关使用 AndroidStudio 进行调试的更多信息)。 否则,请尝试使用 Log.e 函数而不是 Log.d。 Log.e 通常用于错误,在这种情况下,出于测试目的,您可以使用它并确保始终显示您的日志,因为错误日志的优先级高于调试日志。
( optional step
)首先创建一个名为 TAG 的静态/常量变量:
const val TAG = "aaa"
在 Logcat 窗口 -> 过滤器下拉 -> 编辑过滤器配置:
Log Tag:
字段中输入在第一步中选择的名称:您现在可以在 BroadcastReceiver 或项目中的任何其他地方记录您想要的内容:
class BroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
context ?: return
Log.d(TAG, intent?.action!!)
}
}
结果将是这样的:
问题是标签不应该全部大写。 如果您使用“接收”或“接收”,就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.