自定义广播接收器
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("MyBroadcastReceiver", "onReceive: ");
Toast.makeText(context,"this is myReceiver",Toast.LENGTH_SHORT).show();
}
}
注册广播
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.zoukeqing.helloworld">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyBroadcastReceiver">
<intent-filter>
<action android:name="com.example.zoukeqing.helloworld.MY_BROADCAST"/>
</intent-filter>
</receiver>
</application>
</manifest>
发送广播
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d("MainActivity", "onCreate: ");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button)findViewById(R.id.btn_broadcast);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendBroadcast(new Intent("com.example.zoukeqing.helloworld.MY_BROADCAST"));
}
});
}
}
logs
11-22 09:21:47.200 5536-5536/? I/zygote: Not late-enabling -Xcheck:jni (already on)
11-22 09:21:47.239 5536-5536/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
11-22 09:21:47.578 5536-5536/com.example.zoukeqing.helloworld I/InstantRun: starting instant run server: is main process
11-22 09:21:47.679 5536-5536/com.example.zoukeqing.helloworld D/MainActivity: onCreate:
11-22 09:21:47.879 5536-5562/com.example.zoukeqing.helloworld D/OpenGLRenderer: HWUI GL Pipeline
[ 11-22 09:21:47.917 5536: 5562 D/ ]
HostConnection::get() New Host Connection established 0x9a3a9880, tid 5562
11-22 09:21:47.923 5536-5562/com.example.zoukeqing.helloworld I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
11-22 09:21:47.923 5536-5562/com.example.zoukeqing.helloworld I/OpenGLRenderer: Initialized EGL, version 1.4
11-22 09:21:47.923 5536-5562/com.example.zoukeqing.helloworld D/OpenGLRenderer: Swap behavior 1
11-22 09:21:47.924 5536-5562/com.example.zoukeqing.helloworld W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
11-22 09:21:47.924 5536-5562/com.example.zoukeqing.helloworld D/OpenGLRenderer: Swap behavior 0
11-22 09:21:47.934 5536-5562/com.example.zoukeqing.helloworld D/EGL_emulation: eglCreateContext: 0x89bc1ba0: maj 2 min 0 rcv 2
11-22 09:21:47.967 5536-5562/com.example.zoukeqing.helloworld D/EGL_emulation: eglMakeCurrent: 0x89bc1ba0: ver 2 0 (tinfo 0x9cd3d500)
11-22 09:21:48.036 5536-5562/com.example.zoukeqing.helloworld D/EGL_emulation: eglMakeCurrent: 0x89bc1ba0: ver 2 0 (tinfo 0x9cd3d500)
弄半天了,不知道哪里错误