What is the requirement to make use of this:
mMessageContentView = (WebView) findViewById(R.id.message_content);
mMessageContentView.getSettings().setBlockNetworkLoads(false);
Everytime I set setBlockNetworkLoads to false. My application crashes! If I set it to true, it works fine but my application can't load images over the network.
Is there something I need to do before hand before I can set it to false?
As correctly pointed out by Gallal - I should of made use of the logs. A quick command on dos:
adb logcat
Showed that my application was missing the internet permission in the manifest file. So adding the following to my android manifest solved my problem:
<uses-permission android:name="android.permission.INTERNET" />
Related
I am trying to modify my existing contacts on my device.
I have implemented all the related stuff (AuthenticationService, AbstractThreadedSyncAdapter, SyncService, AbstractAccountAuthenticator etc..) following this guides/examples and this.
All works fine on my Samsung Galaxy S7 that has Android 7: now my contacts have a new custom function, if I select it, an Activity of my Application is launched. This is accomplished creating a contacts.xml file and using a custom MIMETYPE (as explained in the links above).
Unfortunately this approach seems to not work on devices running Android with version < 7. My custom function is not shown although everything seems to work correctly and no error is detected.
If someone knew something concerning it, it would be a great help.
Thanks in advance.
I'm assuming the ";" in your contacts.xml is a typo, or your project wouldn't compile
Try removing the android:detailSocialSummary line from your contacts.xml as most examples I know don't have that
Instead of ContactsAccountType try ContactsSource, see this as reference.
The mimetype usually doesn't point to an activity within your app, it should be: vnd.android.cursor.item/vnd.<your package>.<some action> but I don't think that's the problem you're having
Check Mime Type line, you must have to add .profile at the end and /vnd..profile
<?xml version="1.0" encoding="utf-8"?>
<ContactsSource
xmlns:android="http://schemas.android.com/apk/res/android">
<ContactsDataKind
android:mimeType="vnd.android.cursor.item/vnd.com.krishna.test.profile"
android:icon="#mipmap/ic_launcher"
android:summaryColumn="data2"
android:detailColumn="data3" />
</ContactsSource>
I'm trying to make a app with WebView, but the website is using https, but the content (ex. mp3 file) uses http, so Android Lollipop won't load it because it is "Mixed Content". I tried to use onReceivedSslError handler.proceed();, but it doesn't load anything. Is there a way to fix it? or could I just make all websites loaded use http, so It doesn't show any errors?
Since Pie (API 29), all non-HTTPS traffic in app is now disabled by default.
If you're targeting API level 26 or above, you must first enable it in the manifest file. Add
android:usesCleartextTraffic="true"
into <application> tag.
Since Lollipop (API 21), WebView blocks all mixed content by default.
To change this behaviour, when you are targeting API level 21 or above, use:
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
In this mode, the WebView will attempt to be compatible with the
approach of a modern web browser with regard to mixed content. Some
insecure content may be allowed to be loaded by a secure origin and
other types of content will be blocked. The types of content are
allowed or blocked may change release to release and are not
explicitly defined.
In practice this should allow loading of images, videos, music etc. - all content that has low probability of being major security threat, when tampered/replaced by malicious third-party.
Alternatively use (strongly discouraged):
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
In this mode, the WebView will allow a secure origin to load content
from any other origin, even if that origin is insecure. This is the
least secure mode of operation for the WebView, and where possible
apps should not set this mode.
If your min API is less than 21 and cannot call setMixedContentMode directly, you can use reflection:
try {
Method m = WebSettings.class.getMethod("setMixedContentMode", int.class);
if ( m == null ) {
Log.e("WebSettings", "Error getting setMixedContentMode method");
}
else {
m.invoke(webView.getSettings(), 2); // 2 = MIXED_CONTENT_COMPATIBILITY_MODE
Log.i("WebSettings", "Successfully set MIXED_CONTENT_COMPATIBILITY_MODE");
}
}
catch (Exception ex) {
Log.e("WebSettings", "Error calling setMixedContentMode: " + ex.getMessage(), ex);
}
In android pie in addition to setting the mixed content mode, you also need to set the android:usesCleartextTraffic attribute in the AndroidManifest.
In your AndroidManifest.xml do:
<application
....
android:usesCleartextTraffic="true"
...>
and when setting up the webview, do:
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
to load it conditionally on API >= 21, you don't have to use reflection.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
}
I've recently migrated from Crosswalk to use the native WebView.
Had to fight with this issue for a few hours. The fix was to run clearCache() prior to setting the settings.
webView.clearCache(false); // <-- DO THIS FIRST
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
Go to manifest.xml and add the following line.
android:usesCleartextTraffic="true"
And in Java file of webview add this code.
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
If you are running into this problem, just make sure you have installed the Ionic's WebView Cordova Plugin (https://github.com/ionic-team/cordova-plugin-ionic-webview). The easiest way is to check your package.json.
Once installed:
Open your config.xml file
Check if you have an entry for <preference name="Scheme">
If you do, check that the value is "https".
If you don't have it, then add this line:
<preference name="Scheme" value="https" />
Add this line:
<preference name="MixedContentMode" value="0" />
This solved the problem for me.
I built a web app and wants to create an android app that has a webview that shows my web app. After following the instructions from Google Developer to create an app, I successfully installed it on my phone with Android 5.1.1.
However, when I run the app for the first time, the webview shows the message:
Web page not available
The Web page at [Lorem Ipsum URL] could not be loaded as:
net::ERR_CACHE_MISS
I solved the problem by changing my AndroidManifest.xml.
old : <uses-permission android:name="android.permission.internet"/>
new: <uses-permission android:name="android.permission.INTERNET"/>
Answers assembled! I wanted to just combine all the answers into one comprehensive one.
1. Check if <uses-permission android:name="android.permission.INTERNET" /> is present in manifest.xml. Make sure that it is nested under <manifest> and not <application>. Thanks to sajid45 and Liyanis Velazquez
2. Ensure that you are using <uses-permission android:name="android.permission.INTERNET"/> instead of the deprecated <uses-permission android:name="android.permission.internet"/>. Much thanks to alan_shi and creos.
3. If minimum version is below KK, check that you have
if (18 < Build.VERSION.SDK_INT ){
//18 = JellyBean MR2, KITKAT=19
mWeb.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
or
if (Build.VERSION.SDK_INT >= 19) {
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
because proper webview is only added in KK (SDK 19). Thanks to Devavrata, Mike ChanSeong Kim and Liyanis Velazquez
4. Ensure that you don't have webView.getSettings().setBlockNetworkLoads (false);. Thanks to TechNikh for pointing this out.
5. If all else fails, make sure that your Android Studio, Android SDK and the emulator image (if you are using one) is updated. And if you are still meeting the problem, just open a new question and make a comment below to your URL.
For anything related to the internet, your app must have the internet permission in ManifestFile. I solved this issue by adding permission in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
I tried above solution, but the following code help me to close this issue.
if (18 < Build.VERSION.SDK_INT ){
//18 = JellyBean MR2, KITKAT=19
mWeb.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
}
Use
if (Build.VERSION.SDK_INT >= 19) {
mWebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
}
It should solve the error.
Also make sure your code doesn't have true for setBlockNetworkLoads
webView.getSettings().setBlockNetworkLoads (false);
I ran to a similar problem and that was just because of the extra spaces:
<uses-permission android:name="android.permission.INTERNET "/>
which when removed works fine:
<uses-permission android:name="android.permission.INTERNET"/>
I've read every question both here and on Google and can't for the life of me figure this one out. I'm simply trying to use Phonegap to take a picture and store the URI. I'm trying to use the example from the docs here. Here's the snippet where I'm trying to open the camera.
this.getPicture = function() {
navigator.camera.getPicture(self.onBackSuccess, self.onFail, {
quality: 20,
destinationType: Camera.DestinationType.FILE_URI
});
};
What I've already checked/verified
Phonegap/Cordova itself is installed and working fine. I can build my app with no problems.
navigator.camera above is definitely defined. I can follow the code down and the "getPicture" function is being called, but that doesn't pull up the Camera, either on the device, ADB emulator, or Ripple browser emulator.
Neither my success nor my error callback are ever being called. However, when I look at those variables in the getPicture function, I can see the appropriate function stored in those variables.
I have installed the Phonegap camera plugin. When I run phonegap local plugin list, one of the results is org.apache.cordova.camera 0.3.4 "Camera"
My config.xml file contains both the lines <feature name="http://api.phonegap.com/1.0/camera"/> and <gap:plugin name="org.apache.cordova.camera" />. Not sure if that's relevant, since the plugin is already installed.
My AndroidManifest.xml file contains the lines <uses-permission android:name="android.permission.CAMERA" />, <uses-feature android:name="android.hardware.camera" />, and <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />.
When I click on the button that calls this function, it prints out this:
[20,1,1,-1,-1,0,0,false,false,false,null,0]
which is the array of cameraOptions that gets generated/used by the camera.getPicture function.
Everything I've been able to find just takes for granted the fact that calling navigator.camera.getPicture(success, failure, options); is going to open the camera, so there doesn't seem to be much info on solving my particular problem. Any pointers here would be much appreciated.
Edit: I am seeing an error in the console that looks like it may be related:
Could not find cordova.js script tag. Plugin loading may fail.
Edit: I don't think my last edit is relevant. That has to do with the loading of the plugin files themselves, and I can see the Camera.js file in the plugins folder when I load my app. I can put a breakpoint in the cameraExport.getPhoto function and it does go in there. Then it calls
exec(successCallback, errorCallback, "Camera", "takePicture", args);
which prints out the array I mentioned before and does nothing else.
without seeing all of your code, my guess is that the use of this. and self. is causing the issue. Try something like this:
function getPicture() {
navigator.camera.getPicture(onBackSuccess, onFail, {
quality: 20,
destinationType: Camera.DestinationType.FILE_URI
});
};
function onBackSuccess() {
//some code
}
function onFail() {
//some code
}
Android contains a permission called 'ACCESS_LOCATION_EXTRA_COMMANDS'. Normal location commands would involve accessing coarse/fine location. Does anyone know what kind of extra commands this permission allows the app to access ?
Thanks.
I only know of 1 command which can be uses when you have a slow GPS fix:
((LocationManager)YourActivity.this.getSystemService("location")).sendExtraCommand("gps", "delete_aiding_data", null);
and in the Manifest:
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
According to a rough search in Android source code, it indicate that LocationManager.sendExtraCommand() need this permission exactly.
Documentation: sendExtraCommand(java.lang.String, java.lang.String, android.os.Bundle)
Go to https://cs.android.com/android/platform/superproject/+/master:frameworks/base/services/core/java/com/android/server/location/provider/AbstractLocationProvider.java;drc=master;bpv=1;bpt=1;l=353, click on onExtraCommand if you don't see the "References" panel at the bottom, scroll down to "Overriden By", and click on each implementation to see what commands it supports.
Here's a list of commands supported by GnssLocationProvider (since all of the other implementations seem to do nothing or delegate to another one):
delete_aiding_data: calls deleteAidingData
force_time_injection: calls requestUtcTime
force_psds_injection: sends a DOWNLOAD_PSDS_DATA message if mSupportsPsds is true
request_power_stats: calls requestPowerStats