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

自定义广播没有反应,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条回答 默认 最新

  • JPF1024 2017-11-22 15:48
    关注
    评论

报告相同问题?

悬赏问题

  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境