백그라운드에서 활동을 시작 해야하는 Android 앱을 만들고 있습니다. 이를 위해 서비스를 확장하는 ForegroundStarter를 사용하고 있습니다. Foreground 서비스에서 실행해야하는 Adscreen.class 활동이 있습니다. 활동 Adscreen.class는 Android 10을 제외한 모든 Android 버전에서 제대로 작동합니다 (배경에서 시작).
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);
}
}
Android 10의 백그라운드에서 활동을 시작하는 데 약간의 제한이 있음을 읽었습니다.이 코드는 더 이상 작동하지 않는 것 같습니다. 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);
Android 10의 백그라운드에서 활동을 시작하는 해결 방법이 있습니까?