Can not download the file from server in android web view - java

I want to download a file from my school web view app.
When I open my school website in chrome (android) it downloads my marks file, but in my Web view app it does not download. But then my Web view app can download course material inside Web view fine.
I have already implemented the download listener. In debugging i check that clicking on marks button it does not enter in to download block code?

You should set download listener to you web view just like this:
mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLengt[h) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
you can learn more about DownloadListener from here

Related

how to link my blog to the android app that i create?

I have created the application ,in one of the buttons.I need to display the link of the blog,so that the user can see my blog when he clicks on it.thanks!
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Add that to the buttons click handler to launch example.com in the web browser

Posting Image with description using last version of Facebook SDK for Android

I want to post image from local user device with message or description, using Facebook SDK, I viewed some old solutions here in this site, but I want to use the last Facebook sdk version (3.5)
I can post image via this code but I can't add description or message
public void shareImageNow(Activity activity, String message,
Bitmap bitmap, UiLifecycleHelper uiHelper) {
List<Bitmap> images = new ArrayList<Bitmap>();
images.add(bitmap);
if (FacebookDialog.canPresentShareDialog(
activity.getApplicationContext(),
FacebookDialog.ShareDialogFeature.PHOTOS)) {
// Publish the post using the Photo Share Dialog
FacebookDialog shareDialog = new FacebookDialog.PhotoShareDialogBuilder(
activity).addPhotos(images).build();
uiHelper.trackPendingDialogCall(shareDialog.present());
} else {
// The user doesn't have the Facebook for Android app installed.
// You may be able to use a fallback.
}
}
Thanks
You can't via the Share Dialog. All messages should be user generated, and if you're using the share dialog, then there's an option in the dialog for the user to add a message or description.

How can I stop webChromeClient from opening files in the Android Browser

I am developing an Android App using WebView which loads a locally stored HTML5/JQuery Mobile app in the assets folder. The App gets content from a REST service on a domain I own. The content is a mixture of HTML content and downloadable files.
My problem lies when a user clicks on a button to download a file, a Browser window launches and the file is downloaded, which is great. However my App then loses focus and I have a blank browser on screen. I have done a bit of Googling and have read a few answers on Stack Overflow and have adapted my code to set a Download Listener, but this still loads the Browser. Please advise. My code is listed below
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAllowFileAccess(true);
webView.setWebChromeClient(new WebChromeClient());
webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
webView.loadUrl("file:///android_asset/www/index.html");
}
Have you tried using the Android DownloadManager (please see http://developer.android.com/reference/android/app/DownloadManager.html) instead of starting a VIEW Intent? I think that you'd just need to enqueue a download from your DownloadListener.

Android: How to close application opened from my app when all sent to background

Here is my problem. I am developing app that loads some documents from server. I open document in another app via Intent.ACTION_VIEW. This is all working just fine. Problem is that whole app is pin protected so I have to capture events such "sent to background" or "screen lock" to bring up pin screen afterwards and this is not working when another app is opened above mine. So if user opens document then press home button, click on my launch icon from menu then he gets again external app with opened document and with back button access my app again. This is security issue that needs to be fixed.
Here are some code snippets:
Opening document:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(downloadedFile);
String mimeType = document.getMimeType();
intent.setDataAndType(uri, mimeType);
startActivityForResult(intent, 1);
capture sent to background:
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
IN_BACKGROUND = true;
Log.i(PinUtil.class.getSimpleName(), "App sent to background ");
} else {
IN_BACKGROUND = false;
}
}
My question is: Is it possible to detect if my app is sent to background when another app is opened? How not to open another app when my launcher icon is pressed.
Thanks for all responses.
Regards
Lubos
In order to fix this problem:
So if user opens document then press home button, click on my launch
icon from menu then he gets again external app with opened document
and with back button access my app again. This is security issue that
needs to be fixed. Here are some code snippets:
You need to make sure that, when you launch an external app for the user to view a document, that the external app does not run in the same task as your application. It needs to run in a new, separate task. You can do this like this:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // Ensure app runs in separate task
Uri uri = Uri.fromFile(downloadedFile);
String mimeType = document.getMimeType();
intent.setDataAndType(uri, mimeType);
startActivity(intent); // Can't use startActivityForResult() here
However, you can't use startActivityForResult() when you launch the external viewer, because an activity running in another task cannot return a result to you. However, most external applications won't return a result when launched with ACTION_VIEW anyway, so it probably isn't a problem.
Then you asked this:
My question is: Is it possible to detect if my app is sent to
background when another app is opened? How not to open another app
when my launcher icon is pressed.
There should be some answers on StackOverflow that can help you determine if your application is in the background (it isn't actually that easy to determine this).
My explanation above should answer your 2nd question. If you don't launch other apps in your task, then only your app will be launched when your launcher icon is pressed.

Android open browser from service avoiding multiple tabs

I am trying to open the browser window from my service with a link that opens in the current tab of the browser. when I use
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.google.com"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(intent);
If the browser was opened and in the foreground before my service starts the intent and the browser opens the link in the same window/tab. If the Browser was minimized and the service starts the intent, the browser opens the webpage in a new window/tab.
I cannot use a web-view as it needs to be in the web browser and it will only be dealing with the default browser. I have checked out the thread at Open an url in android browser, avoid multiple tabs but it did not have an answer. I also have tried force closing the browser but that is also does not work.
Thank you for any help!
If you want to use your app to fire that intent .. so call your app directly through package name
check the following code:
Intent in = getPackageManager().getLaunchIntentForPackage("com.package.myappweb");
in.putExtra("myURL","http://www.google.com");
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity( in );
you can pass the URL through intent and get it in your app by:
String url=getIntent().getStringExtra("myURL");
if(url!=null){
/// launch app and use one tab for showing the webpage
}else{
//launch app normally.
}
the code you mentioned used for using app as web-app ..that will show popup window to use to pick up any browser to deal with the link .. not that would be for any app wanna open link
good luck,
The way to avoid multiple tabs is as follows:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
intent.putExtra(Browser.EXTRA_APPLICATION_ID, getPackageName());
startActivity(intent);
The EXTRA_APPLICATION_ID will contain an ID of your app, and the browser will recycle a tab that was opened by your application, rather than opening a new tab. In this way, all the pages opened by your application will share the same tab, and you will not have "tab inflation" in the browser.
None of the solutions above involving Browser.EXTRA_APPLICATION_ID work.
However, Google's Custom Tabs which is now the standard for opening URL's was easy to implement and works like a charm. Here's all you need to open a tab:
implementation "androidx.browser:browser:1.4.0"
String url = "https://google.com/";
CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
CustomTabsIntent customTabsIntent = builder.build();
customTabsIntent.launchUrl(this, Uri.parse(url));
More can be found here.

Categories