Android的广播接收器不工作

编程入门 行业动态 更新时间:2024-10-12 03:21:09
本文介绍了Android的广播接收器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图让一个广播接收器的工作。应尽可能的简单,我有我的表现是这样的:

I try to get a broadcast receiver working. Should be as simple as possible, I have my manifest like this:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="schemas.android/apk/res/android" package="com.mytest.intentRec" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name=".mainAct" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.mytest.intentRec.MyIntentRec" android:enabled="true" > </receiver> </application> <uses-sdk android:minSdkVersion="7" /> </manifest>

正如你可以看到我有一个主要活动mainAct,这并没有什么,但发送一旦开始广播:

As you can see I have a main activity mainAct, this does nothing but sending the broadcast once started:

public class mainAct extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.sendBroadcast(new Intent()); } }

和我有一类MyIntentRec,这很简单,因为它可以:

and I have a class MyIntentRec, which is as simple as it could:

public class MyIntentRec extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Log.v("IntentRec", "got it"); } }

我想到的是,当我开始我的应用程序,广播发送和被拾起和日志条目被写入。我没有看到日志条目,我没有看到任何错误。我怀疑有无论是在清单或发送广播错误。我刚刚创建一个空的意图存在,它必须具有一定的性能有一定的意图是什么?

What I expect is that when I start my app that a broadcast is sent and being picked up and that a log entry is written. I don't see that log entry and I don't see any error. I'm suspecting to have either an error in the manifest or in sending the broadcast. I just created an empty intent there, does it need to be some intent with certain properties?

推荐答案

请 setClass 为你的意图,

EX:

public class mainAct extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent i=new Intent("any string"); i.setClass(this, MyIntentRec.class); this.sendBroadcast(i); } }

这就是它的意思是没有任何过滤器意味着它只能由指定其确切的类名意向对象上调用。 [老答案] 您应该注册,你需要在manifest什么样的行动。 例如:

That is what it means " The absence of any filters means that it can be invoked only by Intent objects that specify its exact class name." [Old answer] You should register what kind of actions you need in the manifest. Ex:

<receiver android:name="com.mytest.intentRec.MyIntentRec" android:enabled="true" > <intent-filter> <action android:name="your.intent" /> </intent-filter> </receiver>

发送,

send it,

this.sendBroadcast(new Intent("your.intent"));

更多推荐

Android的广播接收器不工作

本文发布于:2023-11-27 12:54:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638165.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接收器   工作   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!