Start main activity from notification fails

2019-08-23 15:13发布

问题:

my application has a service with foreground notification. The service runs even if the main activity closes. I want to restart the main activity if i click the notification. What I did seems to be correct.

Changes to AndroidManifest.xml:

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

Setting a pending intent in the notification:

    Intent activityIntent = new Intent(this, Muzika.class);
    activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mNotifyBuilder  = new NotificationCompat.Builder(this, CHANNEL_ID)
        .setContentIntent(pendingIntent)

If I click the notification, the splash screen of my app is displayed but that's all. It freezes here and won't start.

In the AndroidManifest.xml I have another inten-filter defined to open music files from file manager. It behaves exactly the same way as when clicking the notification. If application is completely closed or the main activity is running, everything works fine. If only the service is running, splash screen of main activity is shown and then freezes.

In the console (using logcat) I can only see this:

09-20 14:02:31.898   951  2149 I ActivityManager: START u0 {act=android.intent.action.VIEW dat=file:///storage/6D83-F513/Audio/AC-DC/T.N.T..mp3 typ=audio/mpeg flg=0x10000000 cmp=cz.jech.muzika/.Muzika} from uid 10406
09-20 14:03:32.836   951  1060 W ActivityManager: Activity pause timeout for ActivityRecord{34b1a98 u0 cz.jech.muzika/.Muzika t1958}
09-20 14:03:42.849   951  1060 W ActivityManager: Activity stop timeout for ActivityRecord{34b1a98 u0 cz.jech.muzika/.Muzika t1958}

The application is called even with the intent to open a music file. But then it stops. The function onCreate is not called. I have a Log command as the first one in this function and there is nothing in the console output.

What bothers me even more is that it used to work. I reverted back to the versions where it used to work but it didn't help. Since then I upgraded the development tools SDK nad my phone to Android 8 (Oreo). Is it possible that the upgrade broke the functionality? Any idea what else could I check?