我试图让一个广播接收器的工作。应尽可能的简单,我有我的表现是这样的:
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的广播接收器不工作
发布评论