Reputation: 19273
Is it possible to launch a service in App A, from a service (widget) in App B, if app A is installed but has never been opened manually by the user?
I was wondering because it seems that there are many restrictions to apps that hasn't been explicitly started before.
Upvotes: 0
Views: 71
Reputation: 17085
Yes, You can achieve using a widget. Here you go..
I have an widget in App B as below..
public class CustomAppWidgetProvider extends AppWidgetProvider{
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
RemoveControlWidget removeControlWidget = new RemoveControlWidget(context,context.getPackageName(), R.layout.widget_play_layout);
appWidgetManager.updateAppWidget(appWidgetId, removeControlWidget);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
}
And my custom remote view (layout with a button id play_control)
public class RemoveControlWidget extends RemoteViews {
public static final String ACTION_PLAY = "com.example.app.ACTION_PLAY";
public RemoveControlWidget(Context context, String packageName, int layoutId) {
super(packageName, layoutId);
Intent intent = new Intent(ACTION_PLAY);
PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), 100,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
setOnClickPendingIntent(R.id.play_control, pendingIntent);
}
}
Now I have a custom service in my App A which will display a notification.
This is what need to be aded in manifest
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name=".CustomService">
<intent-filter >
<action android:name="com.example.app.ACTION_PLAY"/>
<category android:name="android.intent.category.DEFAULT"></category>
</intent-filter>
</service>
</application>
Upvotes: 1