0

When I try to start an activity from a foreground service and app is not in foreground state and removed from recent items as well, my app stops working without any exception. but works fine when app is in foreground state or main activity is opened.

"Draw over other apps" permission is also granted from app settings.

I'm using oneplus 7 Pro, running on android 11, API 30.

Please help me!!

ForgroundService.java

 public int onStartCommand(Intent intent, int flags, int startId) {

        Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)

                .setContentTitle("Content Title")
                .setContentText("Content Text")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .build();

        startForeground(id, notification);

        Intent activityIntent = new Intent(this, SecondActivity.class);
        activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        this.startActivity(activityIntent);

        return START_STICKY; 
    }

SecondActivity.java

 @RequiresApi(api = Build.VERSION_CODES.O_MR1)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final Window win = getWindow();
    win.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
    win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setTurnScreenOn(true);
    setShowWhenLocked(true);
    setContentView(R.layout.second_activity);
}

second_activity.xml

 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">

<TextView
    android:id="@+id/textView"
    android:layout_width="219dp"
    android:layout_height="80dp"
    android:text="SecondActivity"
    android:textColor="#C50A0A"
    android:textColorLink="#B11212"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.app">

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.BatteryApp">

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.BatteryApp.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".SecondActivity">
    </activity>

    <service android:name=".ForgroundService" />

</application>

Thanks!!

  • 1
    Are you sure there is no exception? Have you checked your logcat? Make sure that you don't filter the logcat because you may miss something important! – David Wasser Jul 23 '21 at 06:55
  • Thank you for your time! yeah I wasn't getting any exception. I'm not sure what was the issue but it worked at last. What I did: Instead of calling the startActivity() from Forground service, I registered a custom Broadcast receiver and sending a broadcast explicitly from Forground service and then custom's broadcast OnReceive() calls startActivity method. It works fine. but I'm still not sure why did calling startActivity from foreground Service doesn't work. any idea on this??? – Sahil Khatri Jul 25 '21 at 08:36
  • Here's the reason: https://developer.android.com/guide/components/activities/background-starts It makes no difference if you have a background `Service` or a foreground `Service`. The rules here still apply. – David Wasser Jul 25 '21 at 15:18

0 Answers0