繁体   English   中英

对发送广播的过程感到困惑——android 初学者

[英]Confused about the process of sending a broadcast — android beginner

我试图通过单击一个按钮来发送一个简单的自定义广播,接收器将在接收到广播时推送一个 toast。 但是当我单击按钮时,logcat 中没有出现错误消息:

2021-01-18 16:23:18.870 480-480/? E/netmgr: Failed to open QEMU pipe 'qemud:network': Invalid argument
2021-01-18 16:23:20.931 485-485/? E/wifi_forwarder: qemu_pipe_open_ns:62: Could not connect to the 'pipe:qemud:wififorward' service: Invalid argument
2021-01-18 16:23:20.931 485-485/? E/wifi_forwarder: RemoteConnection failed to initialize: RemoteConnection failed to open pipe

MainActivity 是:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent("com.example.broadcasttest2.MY_BROADCAST");
                sendBroadcast(intent);
            }
        });
    }

BroadcastReceiver 是:

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Toast.makeText(context, "My Broadcast Received", Toast.LENGTH_SHORT).show();
        throw new UnsupportedOperationException("Not yet implemented");
    }
}

我还在 BroadcastReceiver 中注册了该操作:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcasttest2">


    <application
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.BroadcastTest2"
        >
        <receiver
            android:name=".MyBroadcastReceiver"
            android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="com.example.broadcasttest2.MY_BROADCAST" />
            </intent-filter>
        </receiver>

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

有人可以帮我吗? 非常感谢!

对于应用程序广播通信,您必须在 Activity 中注册广播接收器,在您的情况下,您缺少onCreate中的语法,如下所示:

BroadcastReceiver myReceiver = new MyBroadcastReceiver();
registerReceiver(myReceiver, new IntentFilter('com.example.broadcasttest2.MY_BROADCAST'));

更多参考 - 广播接收器 class 和 registerReceiver 方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM