制作 MainActivity 的布局文件 acitvity_main,其完整代码如下:
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="Received Broadcasts" />
android:id="@+id/btnSendBroadcast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
android:text="Send Broadcast"/>
android:id="@+id/etReceivedBroadcast"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
制作 MainActivity ,其完整代码如下:
package com.ccsoft.broadcastdemo;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements View.OnClickListener {
MyReceiver myReceiver;
IntentFilter intentFilter;
EditText etReceivedBroadcast;
Button btnSendBroadcast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activit_main);
etReceivedBroadcast = (EditText) findViewById(R.id.etReceivedBroadcast);
btnSendBroadcast = (Button) findViewById(R.id.btnSendBroadcast);
//keep reference to Activity context
MyApplication myApplication = (MyApplication) this.getApplicationContext();
myApplication.mainActivity = this;
btnSendBroadcast.setOnClickListener(this);
myReceiver = new MyReceiver();
intentFilter = new IntentFilter("chanchawReceiver");
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(myReceiver, intentFilter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(myReceiver);
}
@Override
public void onClick(View view) {
Intent intent = new Intent("chanchawReceiver");
sendBroadcast(intent);
}
}
上面步骤贴到项目中后会有些类、对象不存在造成报错,先别着急,布置完全部代码后即可正常运行。步骤2中用到的 MyApplication 的完整代码如下
package com.ccsoft.broadcastdemo;
import android.app.Application;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
}
MainActivity mainActivity;
}
接收器 MyReceiver 的完整代码如下:
package com.ccsoft.broadcastdemo;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
private static final String TAG = "chanchaw";
@Override
public void onReceive(Context context, Intent intent) {
MainActivity mainActivity = ((MyApplication) context.getApplicationContext()).mainActivity;
mainActivity.etReceivedBroadcast.append("broadcast: "+intent.getAction()+"\n");
String msg = intent.getStringExtra("msg");
Toast.makeText(context, "接收到了!", Toast.LENGTH_LONG).show();
Log.i(TAG,"接收到广播的消息了!");
}
}
项目的清单文件 AndroidManifest.xml 的完整代码如下:
package="com.ccsoft.broadcastdemo">
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
最终形成的项目结构如下图: