I am building an android app where I need to start an activity from background. I am using a ForegroundStarter which extends Service for accomplishing this. I have an activity Adscreen.class which I need to run from my Foreground service. The activity Adscreen.class works fine(starts from background) on all Android versions except 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);
}
}
I read that there are some restrictions on starting activities from background on Android 10. This code doesnt seem to be working anymore. 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);
Any workarounds to start an activity from background on Android 10?
Not sure if it's right to do it this way, but I did not have enough time (app is only for company internal use).
I used permissions:
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
and then every user has to go to setting -> permissions of the app and then check box in advanced settings for function "show the app over others"
Sorry for my English or not exactly the right solution, but it worked, so good hotfix for me.
As you mentioned Restrictions on starting activities from the background
It is stated that
Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background.
They also mentioned in their note is that.
Note: For the purposes of starting activities, an app running a foreground service is still considered to be "in the background"
This means if you're using a foreground service to start an Activity it still considers the App is in the background and won't launch an app Activity.
Solution: Firstly, You can't start the app if it is running in the background from Android 10 (API level 29) and higher. They have provided a new way to overcome this behavior which is that instead of calling app you can show a high-priority notification with a full-screen intent .
Full-Screen Intent behaves such as if your device screen is Off It will launch your app Activity which you desired. but if your app is in background and screen is on then it will just show a notification. If you click on the notification then it will open your app.
For more information on High-Priority Notification and Full-Screen Intent you can check it here Display time-sensitive notifications
I do not have enough reputation to comment solution https://stackoverflow.com/a/59067224/8995217 so I try to leave my answer on it for MIUI rom
It seems need to grand some permissions for app running on Xiaomi phones. Go to phone settings -> Apps -> Manage apps then find your app.
On app info page go to other permissions and enable the following options
It works for Xiaomi Redmi Note 8T
You should include to AndroidManifest.xml below permission.
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
As android documentation stats that you can't open an activity from background in new api versions-
android doc reference - https://developer.android.com/guide/components/activities/background-starts
still you can do it by making your own custom scheme, it's as same as we are doing in deep linking-
first make a URI and put this code in your service or broadcast receiver
Uri uri = new Uri.Builder().scheme("rating").authority("call").build();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(uri);
i.putExtra("call_ratings_for", call_id);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
and put this code inside your activity which you want to open-
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="rating" />
</intent-filter>
</activity>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.