我的课程看起来像这样:
package com.broadcast;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class BroadcastActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void tasterPritisnut(View target) {
Intent intent = new Intent("akcija");
intent.putExtra("message", "Hello Valakar");
this.sendBroadcast(intent);
}
}
这是广播课。清单类是:
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".BroadcastActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
现在我用新类创建了一个新项目:
package com.reciever;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class Reciever4 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String poruka = intent.getStringExtra("message");
Log.d("", poruka + " " + Thread.currentThread().getName() + " " + Thread.currentThread().getId());
}
}
这是接收器类,清单文件是这样的:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.reciever"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<receiver android:name=".Reciever4">
<intent-filter>
<action android:name="akcija"/>
</intent-filter>
</receiver>
</application>
</manifest>
我安装了两个应用程序,但是当我发送广播时,日志中没有打印任何内容。我已经建立了类似于Apress书籍的例子,但是无法让它发挥作用。我错过了什么?