How to save cookies for my android web application - android

lets say I have an application for my University web site. Where user can navigate by pressing on screen button( Actually, what I did, I categorized everything in my main layout,where each button represents each fields, if any one presses myaccountStatus, then he will be directed to that specific link) However, my web site needs user name and password every time. Where I don't want my user to bother about this at all. So my question is, is there any way I can enable auto-log in?
My application has basic core web feature implemented(WebView, zoom, java-script enabled, some back, forward, stop, reload, favorite, save for offline reading) and everything is working fine, please help with the auto-log in problem.
So far I know, I can do this by saving cookies. But I have no Idea, how they look, what are they and how to handle those thing. So, please give me a step by step tutorial for this problem. Thanks in advance.

If your are using a web-view based approach (maybe something like cordova?) you could just use the local storage or the web sql standard for html5 apps: Tutorial for Web-Sql.
If you want to store cookies you have to enable them. I'm doing this in my Phonegap/Cordova application with this small Lines of code:
#Override
public void onCreate(Bundle savedInstanceState) {
// ... do whatever you want...
// this lines allow to set cookies
try {
CookieManager.setAcceptFileSchemeCookies(true);
} catch (Throwable e) {
}
// and the default stuff for phonegap/cordova to show a splashscreen and load the application
super.onCreate(savedInstanceState);
super.setIntegerProperty("splashscreen", R.drawable.splash);
// load the index url from config adding locale support.
super.loadUrl(Config.getStartUrl() + "?locale="
+ Locale.getDefault().getLanguage(), 4000);
}
I hope this helps.

Related

How to open common incomplete products link with different toolbar in android studio

I have an eCommerce website and that has been running as an android app using WebView method. I have basic knowledge in android studio but not depth. My concern is wanna open Second Activity from Main Activity when user clicks on any product in app that contains link as "https://www.ecommerce.in/product/XXXXX". Here "https://www.ecommerce.in/product/" is common to all products but "XXXXX" will change to every product when user clicks on different products and this Second Activity should open using another toolbar without related to Main Activity toolbar and my code is like this:
webView.setWebViewClient(new ourViewClient()
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if(url.contains("https://www.ecommerce.in/product/XXXXX"))
{
Intent intent = new Intent(MainActivity.this, ProductsActivity.class);
startActivity(intent);
}
}
});
This code is not working at all. Hope, I get a solution for this and thank you for your help in advance.
First, it looks like you're trying to filter out links with a contains:
if(url.contains("https://www.ecommerce.in/product/XXXXX"))
But you've made it too restrictive. It is literally looking for "XXXXX" in the URL. Also, this doesn't really get you much in terms of security. WebViews are very dangerous. I'd suggest looking through this link for some basic suggestions.
Anyways, if you just want to open the ProductActivity it looks like all you have to do is update the test as follows:
if (url.startsWith("https://www.ecommerce.in/product/"))
Note if you need that URL then you have to pass it to the ProductActivity as part of the intent and grab it in the ProductActivity, but be sure to verify the URL before just loading it to make sure sit starts with the common part.
It might be that someone is trying to get your Activity to do things it shouldn't be.

Open a website inside app using corona

I want the user to open website / web page inside app and not in browser. I have a button, which should open a webpage on click.
I am using native.showWebPopup function for this in main.lua. The problem that I am getting is, a white colored page flashes and disappears immediately.
Below is my code. Any help is greatly appreciated. Thanks.
function openLink(event)
if(event.phase == "ended")then
native.showWebPopup("http://www.google.co.in")
end
end
btn:addEventListener("touch", openLink)
Make sure you check that you've provided access to the internet on Android!
Referring to the documentation: http://docs.coronalabs.com/api/library/native/showWebPopup.html
For Android
If the web popup is displaying web pages from the Internet, you must add the INTERNET permission to the build.settings file.
settings =
{
android =
{
usesPermissions =
{
"android.permission.INTERNET",
},
},
}
Other than that, the syntax looks correct, so I suspect it is a permissions problem.
Hope that helps!

Android web view "back" button to load previously loaded div

I will try to explain this as clearly as possible. I have an android app using web view to basically load a webpage as my app. I have everything working great, however the back button seems to be an issue. I have set this page up all on one html page, it will load in a div when certain buttons are clicked to give the feel of a new page without actually having one. I basically want the back button (on the android tablet or smartphone) to load the previously loaded div, but I have no idea where to start with this. Here is what the content switching jquery looks like -
function contentSwitcher(settings){
var settings = {
contentClass : '.contentToLoad',
navigationId : '#sideMenu',
servFront : '#clickHomeHome'
};
//Hide all of the content except the first one on the nav
$(settings.contentClass).not(':first').hide();
$(settings.navigationId).find('li:first').addClass('active');
//onClick set the active state,
//hide the content panels and show the correct one
$(settings.navigationId).find('a').click(function(e){
var contentToShow = $(this).attr('href');
contentToShow = $(contentToShow);
//dissable normal link behaviour
e.preventDefault();
//set the proper active class for active state css
$(settings.navigationId).find('li').removeClass('active');
$(this).parent('li').addClass('active');
//hide the old content and show the new
$(settings.contentClass).hide();
contentToShow.show("slow");
});
}
contentSwitcher();
});
note: I've cropped out a bunch of it just to show how it works on a basic level.
Does anyone have any suggestions as to where to begin. I'd just like the back button function to be able to maybe check a started previous div name stored somewhere and load that.
thanks!
You can try using the History API. There are numerous tutorials on the web e.g. this one is quite good:
http://diveintohtml5.info/history.html
Basically this is how it works. When the user clicks the link for the div to show you push the state to the history stack.
history.pushState({<object with any information about state>}, pageTitle, newUrl);
This will push the state to the history stack meaning that when the user presses the back button on any modern browser like webkit it will take that state into consideration. When back action is taken it will then pop the state from the history stack. This action you have to listen to and handle in any way you see fit:
window.addEventListener("popstate", function(event) {
// event object contains the information from the pushed state
// do whatever needed to load the previous page here
});
The History API requires you to structure your code in a certain way for it to work well. For this I would recommend to use some existing framework that handle the back events for you e.g. Backbone.js. Hope this helps.

Creating a Questionnaire in Android

Peace be on all of you!
I have to develop a questionnaire like small android app. There will be 10 questions with only 2 types of answers, i.e. either "Yes" or "No". When the user will answer all of the 10 questions, a report will be shown to user according to his answers.
Kindly tell me, how should I proceed? Do I need to use database (sqlite) or can work without it? and how should I start to develop this app?
I am new to Android.
Thank-you!
If you are new to Android, than use a web approach: Show a html page 1-10 in a webView and link it each other and finally the 10th is linked to an url, where you will do a http POST / GET with your collected 10 params. Exactly as how would you do in a "standard' web development. Also you can use several app to wrapp into Android app: Appcelerator, Phonegap and so on.
Here is a class which is a screen: (Android Activity)
public class Help extends Activity{
private WebView webViewHelp;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.help);
webViewHelp = (WebView) findViewById(R.id.webViewHelp);
webViewHelp.loadUrl("file:///android_asset/ui/help.html");
}
}
you need the help.xml build and placed into /res/layout/ folder.
Also write the help.html and place it into: /assets/ui folder and not android_asset and at is not file:///assets/ui/help.html!
in this case you have the starting point set up, than go and load with html links the next next next ... until is done, than pust url.
Somewhat easyer if you are doing in android ui development, and not web-like, but that need a bit more experience

How to add android bookmark on homescreen from web page?

I'm new to web applications for Android..
How can you add a bookmark on the home screen from a web page using Javascript from the click of a button?
If we make an easy way for users to bookmark pages, they will show some interest in bookmarking them. This is one of the requirement for my projects.
How can this be done?
Some of the people answering here seem to not understand that you want something for mobile, not IE, Firefox, etc. They also don't seem to read very well where you said "website", not "mobile app". However, I believe I have read your question properly.
There's extremely high odds that you'll probably also want this for iPhone, so I'll answer this for iPhone and Android as well.
For iPhone, it's as simple as using this script:
http://code.google.com/p/mobile-bookmark-bubble/
For Android, it's not so pretty, unfortunately. You'll have to make a button or hyperlink that redirects to an instructions page on your site. The instructions will tell web visitors to do "Settings > More > Add Shortcut to Home" and hope they "get it".
One option for Android is that you can fork mobile-bookmark-bubble, make it load at the bottom of the page without the bottom triangle, and make it read, "Click the Settings button > More > Add Shortcut to Home". As well, you might want to put the 2 bars icon of what the Settings button looks like right next to "Settings button".
As for how to customize the icon that gets created, I'm still researching that and will update this answer when I find out.
Here is a possible workaround, and while it isn't exactly what you want, I think it is the easiest solution as I'm highly skeptical that android allows web pages to automatically issue intents to a device, as this would be a potential security problem. Someone please correct me if I'm wrong
I recommend that you create an app that is simply a wrapper around a bookmark. When the user clicks on your app, your app creates an intent that simply opens the device's default web browser to your page. While this will require the users install a app it has a few advantages over a plain bookmark. You will be able to track how many people have your app/bookmark installed, how often they use it etc. You can also provide a nice looking icon instead of using the stock "bookmark" icon.
Upload the app to the market as a free app and place a "bookmark this" link on your webpage that simply directs the user to download your free app.
You can create a web page shortcut on home screen using this code. Provide the necessary info like url, title etc..
final Intent in = new Intent();
final Intent shortcutIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
long urlHash = url.hashCode();
long uniqueId = (urlHash << 32) | shortcutIntent.hashCode();
shortcutIntent.putExtra(Browser.EXTRA_APPLICATION_ID, Long.toString(uniqueId));
in.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
in.putExtra(Intent.EXTRA_SHORTCUT_NAME, title);
in.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(
BrowserBookmarksPage.this,
R.drawable.ic_launcher_shortcut_browser_bookmark));
in.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
//or in.setAction(Intent.ACTION_CREATE_SHORTCUT);
sendBroadcast(in);
Update
The browser does not recognise the intent scheme, so there is no way you can add a shortcut to your webpage from your webpage.
This can help
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("a.jQueryBookmark").click(function(e){
e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
var bookmarkUrl = this.href;
var bookmarkTitle = this.title;
if (window.sidebar) { // For Mozilla Firefox Bookmark
window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
} else if( window.external || document.all) { // For IE Favorite
window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
} else if(window.opera) { // For Opera Browsers
$("a.jQueryBookmark").attr("href",bookmarkUrl);
$("a.jQueryBookmark").attr("title",bookmarkTitle);
$("a.jQueryBookmark").attr("rel","sidebar");
} else { // for other browsers which does not support
alert('Your browser does not support this bookmark action');
return false;
}
});
});
</script>
<h2>Click on the respective links below to bookmark the same</h2>
DeveloperSnippets, Tech Video Bytes, Witty Sparks, Snehah.com
For more info, go to this link ( http://www.developersnippets.com/2009/05/10/simple-bookmark-script-using-jquery/)

Categories

Resources