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

自定义广播没有反应,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);

    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置