Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share data between activity and widget?

I read the hellowidget tutorial and Dev Guide' App Widgets. Then I know how to create a widget which contains button or text or something.

But what I really want to do is making it interact with my app. For example, I want to create a widget that has a text view, and when I click it, it sends a PendingIntent to my activity in which I can edit the text.

I can do the step "sends a PendingIntent". But after I edit text in acitivy, how does the widget read it?

like image 988
Lai Yu-Hsuan Avatar asked May 06 '11 16:05

Lai Yu-Hsuan


People also ask

How do you transfer data from one activity to another?

We can send the data using the putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How pass data from activity to services in Android?

Using putExtra() method, we can send the data. While using it, we need to call setResult() method in services. We can also store data in a common database and access it on services as well as in Activity.

How can I share data between two apps on Android?

Android uses the action ACTION_SEND to send data from one activity to another, even across process boundaries. You need to specify the data and its type. The system automatically identifies the compatible activities that can receive the data and displays them to the user.

How do you pass a parameter to a widget in flutter?

Steps to Pass Data to Stateful Widget in Flutter To pass data to stateful widget, first of all, create two pages. Now from the first page open the second page and pass the data. Inside the second page, access the variable using the widget. For example widget.


1 Answers

What you need to do is register a custom intent, for example ACTION_TEXT_CHANGED in your AppWidgetProvider like this for example:

public static final String ACTION_TEXT_CHANGED = "yourpackage.TEXT_CHANGED";

After this, you need to register in your AndroidManifest.xml that you want to receive this intents in the intent-filter section of your receiver tag like this:

<receiver android:name=".DrinkWaterAppWidgetProvider">
    <intent-filter>
        <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        <action android:name="yourpackage.TEXT_CHANGED" />                
    </intent-filter>
    <meta-data android:name="android.appwidget.provider"
        android:resource="@xml/appwidget_info" />
</receiver>

Then you have to extend the onReceive method in your AppWidgetProvider and make sure that you're handling your intent like this:

@Override
public void onReceive(Context context, Intent intent) {
    super.onReceive(context, intent);
    if (intent.getAction().equals(ACTION_TEXT_CHANGED)) {
        // handle intent here
        String s = intent.getStringExtra("NewString");
    }
}

After all the above is set up, you just need to broadcast the intent in your activity after the text has changed like this:

Intent intent = new Intent(YourAppWidgetProvider.ACTION_TEXT_CHANGED);
intent.putExtra("NewString", textView.getText().toString());
getApplicationContext().sendBroadcast(intent);

Where "NewString" should be changed to the name you to give the the string.

I hope it helps.

like image 158
Henrique Rocha Avatar answered Sep 23 '22 05:09

Henrique Rocha