Sending Intent from widget into another app (Android)

I share my experiences, because not every developer is faced with the development of each and every features in Android.
One day I had the idea to add to my application a widget. But making it as a completely separate program.
What is it for? Well, suppose that you want to sell some parts of the application separately. Maybe it will be chat, calendar widget, weather widget, etc.
How to make a connection for a separate widget with the main application? In fact, without hesitation, comes to mind is elementary simple solution — using Intents, Receivers and Action.
And now I’ll show you how with minimum work time to connect separately developed widget to your application.
Create a widget from Android studio’s template. Something like this:
public class SomeWidget extends AppWidgetProvider {static void updateAppWidget(Context context, AppWidgetManager
appWidgetManager,int appWidgetId) {
RemoteViews views = new
RemoteViews(context.getPackageName(),
R.layout.some_widget);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager
appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
}
@Override
public void onDisabled(Context context) {
}
}
A general template. Easy.
Now we make:
- Add new element to main layout of our widget (R.layout.some_widget)
- Assign ID to this element (no matter what it is, button, text box or image)
- Because the widget is a Receiver, then we have to add the name of the Action and register it in the manifest in the tag <intent-filter> (the template is already done, but I advise you to make a new Action). Like this: com.example.myApp.SOME_BUTTON_PRESSED
Next, work with code. As an example I added a button (R.id.button):
public class SomeWidget extends AppWidgetProvider {static void updateAppWidget(Context context, AppWidgetManager
appWidgetManager,int appWidgetId) {
RemoteViews views = new
RemoteViews(context.getPackageName(),
R.layout.some_widget);
Intent intent = new Intent();
intent.setAction("com.example.myApp.SOME_BUTTON_PRESSED");
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context,0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.button, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
@Override
public void onUpdate(Context context, AppWidgetManager
appWidgetManager, int[] appWidgetIds) {
for (int appWidgetId : appWidgetIds) {
updateAppWidget(context, appWidgetManager, appWidgetId);
}
}
@Override
public void onEnabled(Context context) {
}
@Override
public void onDisabled(Context context) {
}
}
What we did:
- Created an Intent and assign to it our Action
- Wrapped our Intent in PendingIntent with flag FLAG_UPDATE_CURRENT
- Set onClickPendingIntent to our “button”.
Next, we will write an OnReceive method (because our widget is Receiver).
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction()
.equals("com.example.myApp.SOME_BUTTON_PRESSED"))
{
Intent toMainApp = new Intent();
toMainApp.setAction
("com.example.anotherApp.SOME_BUTTON_PRESSED");
context.sendBroadcast(toMainApp);
}
}
Here, we create new Intent and set to it an Action, which will be received by another application and send it.
After it, we just create in the main app (another app) new Receiver with Action com.example.anotherApp.SOME_BUTTON_PRESSED and handle it.
That’s all.
Kind regards.