为什么onReceive在Android中不被称为扩展BroadcastReceiver

为什么onReceive在Android中不被称为扩展BroadcastReceiver,第1张

概述由于某种原因,不会调用onReceive方法我没有应用任何过滤器或任何东西,这是班级:importjava.util.ArrayList;importjava.util.Calendar;importandroid.content.BroadcastReceiver;importandroid.content.Intent;importandroid.content.Context;importandroid.util.Lo

由于某种原因,不会调用onReceive方法
我没有应用任何过滤器或任何东西,

这是班级:

import java.util.ArrayList;import java.util.Calendar;import androID.content.broadcastReceiver;import androID.content.Intent;import androID.content.Context;import androID.util.Log;public class RegisterListener extends broadcastReceiver {private final int DELAY_TIME = 5;private int m_seconds = 0;private int m_minutes = 0;private int m_hours   = 0;static ArrayList<broadcastReceiverListener> Listeners = new ArrayList<broadcastReceiverListener>();@OverrIDepublic voID onReceive(Context context, Intent intent) {    Log.d("ScreenReceiver Action:",intent.getAction());    Calendar c = Calendar.getInstance();     m_seconds = c.get(Calendar.SECOND);    m_minutes = c.get(Calendar.MINUTE);    m_hours = c.get(Calendar.HOUR);    for (broadcastReceiverListener Listener:Listeners){           Listener.onReceive(m_hours, m_minutes, m_seconds);        }}public static voID addbroadcastReceiveListener (broadcastReceiverListener Listener){    if(!Listeners.contains(Listener)){        Listeners.add(Listener);    }}public voID removebroadcastReceiveListener (broadcastReceiverListener Listener){    if(Listeners.contains(Listener)){        Listeners.remove(Listener);    }}

这是清单中的声明(应用程序的子代)

<receiver androID:name=".RegisterListener"></receiver>

编辑:
这是我试图添加的过滤器(在onReceive()内)

    IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);    broadcastReceiver mReceiver = new RegisterListener();    registerReceiver(mReceiver, filter);

我收到一个错误:

The method registerReceiver(broadcastReceiver, IntentFilter) is undefined for the type RegisterListener

解决方法:

如果您总是想听ACTION_USER_PRESENT,请尝试:

<receiver androID:name=".RegisterListener">    <intent-filter>        <action androID:name="androID.intent.action.USER_PRESENT" />    </intent-filter></receiver>

说明

The method registerReceiver(broadcastReceiver, IntentFilter) is undefined for the type RegisterListener

您必须将registerReceiver()与Context一起使用,例如context.registerReceiver(…).但:

This is the filter i trIEd to add (insIDe the onReceive() )

在onReceive()中注册接收器是没有意义的,因为onReceive()在注册接收器之前不会运行…所以它永远不会发生.您可以将此代码放在Activity中的onCreate()中,如下所示:

IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT);broadcastReceiver mReceiver = new RegisterListener();registerReceiver(mReceiver, filter);

这只会在您的应用已经投放时监听ACTION_USER_PRESENT.

总结

以上是内存溢出为你收集整理的为什么onReceive在Android中不被称为扩展BroadcastReceiver全部内容,希望文章能够帮你解决为什么onReceive在Android中不被称为扩展BroadcastReceiver所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1111642.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存