Question

I have created a broadcaster with one Activity in which when I click the button the intent in broadcasted.

The problem is that I would like to broadcast a message which is typed in EditText contained in the GUI not just empty 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>
Était-ce utile?

La solution

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. See the JavaDoc.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top