Estoy creando una aplicación de Android donde necesito comenzar una actividad desde el fondo. Estoy usando un ForegroundStarter que extiende el Servicio para lograr esto. Tengo una actividad Adscreen.class que necesito ejecutar desde mi servicio de primer plano. La actividad Adscreen.class funciona bien (comienza desde el fondo) en todas las versiones de Android, excepto Android 10.
ForeGroundStarter.class
public class ForeGroundStarter extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("sK", "Inside Foreground");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("sK", "Inside Foreground onStartCommand");
Intent notificationIntent = new Intent(this, Adscreen.class);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification =
null;
//Launching Foreground Services From API 26+
notificationIntent = new Intent(this, Adscreen.class);
pendingIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String NOTIFICATION_CHANNEL_ID = "com.currency.usdtoinr";
String channelName = "My Background Service";
NotificationChannel chan = null;
chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.nicon)
.setContentTitle("")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
Intent dialogIntent = new Intent(this, Adscreen.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
Log.d("sk", "After startforeground executed");
}
else //API 26 and lower
{
notificationIntent = new Intent(this, Adscreen.class);
pendingIntent =
PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification =
new Notification.Builder(this)
.setContentTitle("")
.setContentText("")
.setSmallIcon(R.drawable.nicon)
.setContentIntent(pendingIntent)
.setTicker("")
.build();
startForeground(2, notification);
Intent dialogIntent = new Intent(this, Adscreen.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
}
return super.onStartCommand(intent, flags, startId);
}
}
Leí que hay algunas restricciones para iniciar actividades desde el fondo en Android 10. Este código ya no parece funcionar. https://developer.android.com/guide/components/activities/background-starts
Intent dialogIntent = new Intent(this, Adscreen.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
¿Alguna solución para comenzar una actividad desde segundo plano en Android 10?