简体   繁体   English

如何播放某个消息

[英]How to broadcast a certain message

I have created a broadcaster with one Activity in which when I click the button the intent in broadcasted. 我创建了一个带有一个Activity的广播器,当我点击按钮时,广播的意图。

The problem is that I would like to broadcast a message which is typed in EditText contained in the GUI not just empty Intent 问题是我想广播一个消息,该消息在GUI中包含的EditText中输入,而不仅仅是空Intent

public void broadcastIntent(View view) {
    sendBroadcast(new Intent("com.example.MESSAGE_INTENT"));//how to insert a string message in it
}

Activity's Layout: 活动的布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.broadcaster.MainActivity$PlaceholderFragment" >

    <EditText
        android:id="@+id/textField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="19dp"
        android:onClick="clearTextField"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textField"
        android:layout_alignRight="@+id/textField"
        android:layout_below="@+id/textField"
        android:onClick="broadcastIntent"
        android:text="Broadcast message!" />

</RelativeLayout>

You can use the Intent.putExtra() methods to add data to the intent, and the receivers can use the corresponding Intent.getXxx() methods to get the data. 您可以使用Intent.putExtra()方法向intent添加数据,接收方可以使用相应的Intent.getXxx()方法来获取数据。 See the JavaDoc . 请参阅JavaDoc

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

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