1

My Problem as follows:

Since I added into webview "file:///android_asset/index.html" to load from, it crashes at startService(new Intent(this, TimeService.class));. If startService commented out. It works fine. When it is used, but I give webview a address like www.google.de, everything works fine. I am using API Level 26 for developing. Unfortunately I can not resolve this issue by now.

Java Code:

    MainActivity.webView.loadUrl("file:///android_asset/index.html");
    MainActivity.webView.getSettings().setJavaScriptEnabled(true);
    MainActivity.webView.getSettings().setUseWideViewPort(true);
    MainActivity.webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            //tel nummer support
            if (url.startsWith("tel:") || url.startsWith("sms:") || url.startsWith("smsto:") || url.startsWith("mms:") || url.startsWith("mmsto:"))
            {
                Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
                startActivity(intent);
                return true;
            }

            //email support
            if(url.startsWith("mailto:")){
                Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
                startActivity(i);
            }

            view.loadUrl(url);
            return true;
        }
    });

    startService(new Intent(this, TimeService.class));

Crash message:

FATAL EXCEPTION: Thread-6
Process: com.feuerwehrmautern.ffmautern, PID: 3656
android.os.FileUriExposedException: file:///android_asset/index.html exposed beyond app through Intent.getData()
    at android.os.StrictMode.onFileUriExposed(StrictMode.java:1978)
    at android.net.Uri.checkFileUriExposed(Uri.java:2371)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:10247)
    at android.content.Intent.prepareToLeaveProcess(Intent.java:10201)
    at android.app.PendingIntent.getActivity(PendingIntent.java:347)
    at android.app.PendingIntent.getActivity(PendingIntent.java:309)
    at com.feuerwehrmautern.ffmautern.TimeService.sendNotification(TimeService.java:241)
    at com.feuerwehrmautern.ffmautern.TimeService.notificationStuff(TimeService.java:133)
    at com.feuerwehrmautern.ffmautern.TimeService.access$100(TimeService.java:41)
    at com.feuerwehrmautern.ffmautern.TimeService$TimeDisplayTimerTask$1$2.run(TimeService.java:223)
    at java.lang.Thread.run(Thread.java:764)

Service Classs sendNotification

public synchronized void sendNotification(String headtext, String bodytext) {
    synchronized(TimeService.class) {

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Preferences.HOMEPAGE));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        if (notificationID > 999) {
            notificationID = 1;
        }

        NotificationManager mNotificationManager;
        Notification notification;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            CharSequence name = Preferences.ANDROID_CHANNEL_NAME;
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(Preferences.CHANNEL_ID, name, importance);
            channel.setDescription(Preferences.CHANNEL_DESCRIPTION);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            mNotificationManager = getSystemService(NotificationManager.class);
            mNotificationManager.createNotificationChannel(channel);
            notification = new Notification.Builder(TimeService.this, Preferences.CHANNEL_ID)
                    .setContentIntent(pendingIntent)
                    .setContentTitle(headtext)
                    .setContentText(bodytext)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setStyle(new Notification.BigTextStyle().bigText(bodytext))
                    .build();

        } else {
            mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notification = new Notification.Builder(TimeService.this)
                    .setContentIntent(pendingIntent)
                    .setContentTitle(headtext)
                    .setContentText(bodytext)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setStyle(new Notification.BigTextStyle().bigText(bodytext))
                    .build();

            Uri notificationRingtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notificationRingtone);
            r.play();
        }

        mNotificationManager.notify(new Random().nextInt(99999 + 1), notification);

       // System.out.println("Notification " + headtext + " notification NR: " + notificationID);

        incrementNotificationID();

    }
}
Age Doc
  • 11
  • 2
  • The error isn't here. You're not putting the file URI into an Intent here. You're doing that in `TimeService.sendNotification`. You can see this in the stack trace. Post the code that's in your service. *Off topic:* You're missing a `return true` in the `mailto` handling. – Eugen Pechanec Jan 03 '19 at 20:44
  • I added the sendNotification above. – Age Doc Jan 04 '19 at 00:47
  • Possible duplicate of [Navigating asset based html files in webview on Nougat](https://stackoverflow.com/questions/40560604/navigating-asset-based-html-files-in-webview-on-nougat) – Martin Zeitler Jan 04 '19 at 01:17
  • Does the notification actually do anything when you click it on Android below O? What you're saying is "open `file:///android_asset/index.html` in any app that can do such thing". It's an asset Uri, it's relative to the running app. If Chrome handled the intent, it would look in its own assets for `index.html`. That's definitely not what you wanted. So what exactly do you want to happen when the notification is clicked? – Eugen Pechanec Jan 04 '19 at 10:24
  • thanks for the update. When I click this notification, there should be opened a Website. not the local one. really a www.google.com for example. – Age Doc Jan 04 '19 at 10:52

1 Answers1

-1

Starting from api v24 file: scheme is forbidden. Check this solution.

Arthur
  • 769
  • 7
  • 17