在收听台钟报警意图时,Logcat无法在广播接收器中的onReceive内工作

时间:2016-04-21 17:49:08

标签: android broadcastreceiver intentfilter

我正在使用我的广播接收器收听deskclock警报更改意图。当我的广播接收器中的onReceive()方法被调用时,(Log.i/v())内的日志onReceive没有在Android显示器上打印,但Toasts正常工作。

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.dumbrella.ratemyday">

<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <receiver android:process=":remote" android:name="AlarmClockReceiver">
        <intent-filter>
            <action android:name="com.android.deskclock.ALARM_DISMISS" />
            <action android:name="com.android.deskclock.ALARM_SNOOZE" />
            <action android:name="com.android.deskclock.ALARM_ALERT" />
            <action android:name="com.android.deskclock.ALARM_DONE" />
        </intent-filter>
    </receiver>
</application>
</manifest>

广播接收器:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import java.text.SimpleDateFormat;

public class AlarmClockReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        // This log.v does not get printed on Android Monitor
        Log.v("Broadcast Test", "In Broadcast Listener");

        String message = "Broadcast intent detected "
            + intent.getAction();

        // This toast gets displayed after the alarm is dismissed  
        Toast.makeText(context, message,Toast.LENGTH_LONG).show();
    }
} 

3 个答案:

答案 0 :(得分:2)

也许已经太晚了,但我把它留给了这个问题的未来程序员,我遇到了类似的问题并以这种方式解决了。
只需在 Logcat 面板中,将过滤列表框更改为“无过滤器”

答案 1 :(得分:0)

您可能希望确保在DDMS透视图中选择了正确的设备,同时确保选择了正确的过滤选项和日志级别 - 在您需要的情况下选择&#34;详细&#34; 。另请查看选定的答案和建议Logcat is not displaying logs

答案 2 :(得分:0)

您可以在LogCat过滤器配置部分中过滤日志以显示BroadcastReceiver日志:

enter image description here

完整的解决方案步骤:Logs/logcat not working in BroadcastReceiver