我脱下短袖 2017-11-22 14:50 采纳率: 0%
浏览 4230

自定义广播没有反应,Toast弹不出来

自定义广播接收器

 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)

弄半天了,不知道哪里错误

  • 写回答

3条回答 默认 最新

  • pangpangdexiaojiuwo 2017-11-23 02:24
    关注

    你没设置action啊 intent setAction(“com.example.zoukeqing.helloworld.MY_BROADCAST”);
    sendBroadcast(intent);

    评论

报告相同问题?

悬赏问题

  • ¥100 高价邀请复制 域天d8联网狗
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?