Android Studio - сбой службы после того, как WebView использует файл: /// для отображения локального веб-сайта

1

Моя проблема в следующем:

Так как я добавил в веб-просмотр "file:///android_asset/index.html" для загрузки, он зависает при startService(new Intent(this, TimeService.class)); , Если startService закомментирован. Работает нормально. Когда он используется, но я даю webview такой адрес, как www.google.de, все работает нормально. Я использую API Level 26 для разработки. К сожалению, я не могу решить эту проблему до сих пор.

Java-код:

    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));

Аварийное сообщение:

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)

Сервис Класса 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();

    }
}
Теги:
android-studio

1 ответ

0

Начиная с файла api v24: схема запрещена. Проверьте это решение.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню