And*_*oid 11 android broadcastreceiver android-intent
我试图让广播接收器工作.应该尽可能简单,我有这样的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/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>
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我有一个主要活动mainAct,这只会在启动后发送广播:
public class mainAct extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.sendBroadcast(new Intent());
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个MyIntentRec类,它尽可能简单:
public class MyIntentRec extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.v("IntentRec", "got it");
}
}
Run Code Online (Sandbox Code Playgroud)
我期望的是,当我启动我的应用程序时,广播被发送并被拾取并且写入了日志条目.我没有看到日志条目,我没有看到任何错误.我怀疑在清单中发送错误或发送广播.我刚刚在那里创建了一个空的意图,是否需要某些属性的某些意图?
Jet*_*ieh 14
请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);
}
}
Run Code Online (Sandbox Code Playgroud)
这就是它的含义"没有任何过滤器意味着它只能由指定其确切类名的Intent对象调用."
[旧答案]
您应该在清单中注册您需要的操作类型.
例如:
<receiver android:name="com.mytest.intentRec.MyIntentRec" android:enabled="true" >
<intent-filter>
<action android:name="your.intent" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
发送,
this.sendBroadcast(new Intent("your.intent"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42385 次 |
| 最近记录: |