LoadUrl in PhoneGap and Android - android

I am working on an app for android using phonegap and unity.
I wrote a plugin for phonegap that starts a new activity from phonegap that loads the unity player. When i hit the back button in the unity activity i want to go back to the phonegap activity and load my webcontainer again, but with a different url.
Whatever i try, it still loads the URL i loaded the first time. Is there any way to fix that?
Here is the Activity:
public class PhonegapCallsActivity extends DroidGap {
private static Boolean backButton;
private static String url;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras == null) {
Log.d("false","ERROR");
//load index.html when i enter first time
super.loadUrl("file:///android_asset/www/index.html");
return;
}
else{
backButton = extras.getBoolean("backButton");
//load #two when i come back
Log.d("true","ERROR");
super.loadUrl("file:///android_asset/www/index.html#two");
}
}
Note that logcat is tracing true and false correctly, so my variables are set properly.
I always get the index.html loaded, never index.html#two. Is it stored somewhere?

You are doing it right but you are running into an Android bug. Go star:
https://code.google.com/p/android/issues/detail?id=17535
https://code.google.com/p/android/issues/detail?id=17327
In the mean time the best way to work around this issue is to store a flag in localStorage to check if you should be loading page one or page two in the JavaScript.

Related

Can I combine these two apps (native and hybrid) into one app?

I have an app fully developped with Android Studio. I need to create a second app and this one I would like to do it with phonegapp. These two apps are related one to each other... so ideally I would like to combine them both in the same app... By combining I do not mean they need to work together. I would like them to be downloadad together (as the same app) and be able to go from one to the other withinn the app... so they can be completely two separate apps, but where the user would think it is only one app. I hope I am making myself well understood, The hybrid app uses the microphone with the Cordova plugin. At one time I thorugh about placing it with an iframe, but we would lose the microphone which invokes speech recognition Google native in Android, to convert it to text and pass it to edit text.
So my questions is:
Can I combine these two apps (native and hybrid) into one app?
Would the microphone with speech recognition to convert to text work well in both cases?
Yes, it was a bit of a pain to set up, but I have it working. I am not using PhoneGap, but I am using Construct along with the Cordova plugin, so it should be a similar setup.
First I took all of the web files generated by Cordova and placed them in the assets directory (if you do not have an assets directory you can just create one in the main directory). Then I used a WebView to display the content:
public class CordovaActivity extends AppCompatActivity {
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cordova);
mWebView = (WebView) findViewById(R.id.content);
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
settings.setDomStorageEnabled(true);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
settings.setAllowUniversalAccessFromFileURLs(true);
}
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
settings.setMediaPlaybackRequiresUserGesture(false);
}
mWebView.loadUrl("file:///android_asset/www/index.html");
}
#Override
public void onResume() {
super.onResume();
if(mWebView != null) {
mWebView.resumeTimers();
mWebView.onResume();
}
}
#Override
public void onPause() {
super.onPause();
if(mWebView != null) {
mWebView.pauseTimers();
mWebView.onPause();
}
}
}
The setJavaScriptEnabled(), setDomStorageEnabled() and setAllowUniversalAccessFromFileURLs() settings all had to be set to true or the content would not load. The last setting, setMediaPlaybackRequiresUserGesture(), was required to allow audio to play.
It will be a little more work to get the Cordova content to interact with the rest of the app though; you will have to use a JavaScriptInterface in order to do so.
1) Nothing is stopping you from having two apps that act as one (aside from the downloading part). They can "speak" to one another using dedicated Intents and share data using ContentProviders. You will have to take care of the case where one app is installed and the other isn't.
2) I don't see any particular issue with the the microphone in this scenario.
However, if I were you, I'd opt for using a single app if that is possible. I am not sure what are the phonegapp limitations for this kind of implementation though.

Android twitter connection/authentication intent bad formed?

I've been trying to connect to twitter through android for a while.
I'm currently using this api: http://kenai.com/projects/twitterapime/forums/forum/topics/13327-Twitter-API-ME-1-8-Now-with-OAuth-support
I used their exemple with my keys and everything connects without problem. So no problem with configuration.
But in my project which has exactly the same code I can't make it work.
I believe its related with the creation of a new Intent (also this is where the eclipse is point the error)
Im trying to use a custom layout but I don't know what do I need to change to make this specific Twitter layout to work. On main activity I have a button with this:
public void onClick(View v)
{
startActivity(new Intent(this,Twitter.class));
}
on twitter class
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.webView = (WebView) findViewById(R.id.webView1);
WebView myWebView = new WebView(this);
setContentView(myWebView);
authenticate();
}
private void authenticate()
{
WebViewOAuthDialogWrapper page = new WebViewOAuthDialogWrapper(webView);
page.setConsumerKey(CONSUMER_KEY);
page.setConsumerSecret(CONSUMER_SECRET);
page.setCallbackUrl(CALLBACK_URL);
page.setOAuthListener(this);
page.login();
}
what requirements/adjustments are needed to make/change to the Twitter activity?
I believe I need to change something in the intent-filter but I don't even understand the work of intents. I've been only making easy layouts.
Note that I also gave internet permissions already.
Looks like i was making 2 mistakes.
First i was trying to use "webView" variable in WebViewOAuthDialogWrapper instead
"myWebView" so it wasn't initialized...
Second looks like i forgot to put the order of which the external libraries were loaded....
So be sure to put external libraries on top in eclipse project.
Lost about 2 days with these problems... oh well

In phone gap html page take too much time to load

Hi I am making application using phone gap in which certain html pages in asset folder of project. and I am using the code below. but it takes too much time in loading.
can any one give proper solution ?
Thanks!
public class JvdActivity extends DroidGap {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
KeyBoard keyboard = new KeyBoard(this, appView);
appView.addJavascriptInterface(keyboard, "KeyBoard");
super.loadUrl("file:///android_asset/www/index.html");
}
}
If their are lots Images inside the HTML page then to load that page Inside the screen It takes Time.
But if it is requirement of your project then you will use progress bar saying Loading.. so that user can wait till the page gets loaded.

Android: SharedPreference errors after force close

I'm getting some weird effects after I force close my app. When the app is closed with finish(), everything is fine. I have some variables saved in a sharedPreferences so when the app is loaded again, it can restore those variables into the UI. However, if I force close the app and THEN try to continue where it had left off, some variables start "acting funny". By that I mean (in onCreate) I check to see if a string, loaded from the sharedPreferences, equals a value (crunched down version):
String namec;
private static final String TAG = "MyActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//namec was set as "forest" in a previous activity
//which is bypassed if the user selects continue
//from the main menu
SharedPreferences pathtaken = getSharedPreferences("pathtakenpref", MODE_WORLD_READABLE);
namec = pathtaken.getString("namec", "Unknown");
ImageView v1 = (ImageView) findViewById(R.id.pathpic1);
RelativeLayout v2 = (RelativeLayout) findViewById(R.id.pathmain);
Log.i(TAG, "namec= " + namec);
if(namec == "forest"){
v1.setImageResource(R.drawable.forest);
v2.setBackgroundResource(R.drawable.forestrepeat);
}
}
What happens here is namec, does in fact, equal "forest". I send the value to the log and it shows the variable exactly as it should be ("forest"). Yet it won't run the code inside of the if{}. It's giving me nightmares. I've been stuck on this for a week!
In the same code, I load a different set of sharedPreferences (labeled as TRHprefs) and each one of those (6 integers and 3 strings) load up and display just fine. I even add an if{} to test 1 string and 1 integer from TRHprefs... they both came back true.
Q.1: Is there anything that can cause my sharedPreferences xml to become, somehow, corrupted on a force close?
Q.2: Is there a way for me to view the xml file before and after I use force close to help debug the situation. Thanks so much!
Its a String. Try this:
if("forest".equals(namec)){
If you want to compare two String you need to use this:
if(namec.equals("forest")){
v1.setImageResource(R.drawable.forest);
v2.setBackgroundResource(R.drawable.forestrepeat);
}

Android: WebView's method goBack() shows a blank page

I have an android application that loads web pages in an activity with a WebView. I am using the retrieving the page manually and using WebView's loadDataWithBaseURL to display it on screen. Everything there is fine.
Now, i am trying to override the Back button press to simulate going back in the WebView history stack. I am able to override the Back button press, i can see that there is a history stack in the WebView, i can see that the history url is correct, but when i call WebView's goBack() method, it displays a blank page.
Anyone encountered this before or give me a couple of suggestions to proceed from this?
Edit: If i use WebView's loadUrl method, the Back button with an override works as intended. But why.... If i need to handle this manually, how do i start messing with history pages?
I got the same problem also. I found that the problem went away if I set the historyUrl parameter on the call to loadDataWithBaseURL.
You should check if the canGoBack() method returns true before calling goBack()
The only solution I've found is to create a Stack<String> and manually manage history
The way I deal with this is keeping a local stack pointer to the number of loaded pages after the root page loaded using loadDataWithBaseURL . When going back, if my pointer hits 1 I am at root level and reload the root page with loadDataWithBaseURL.
FYI, I use this code in Activities with fragments, so the fragments implement the interface IBackButtonListener which helps me to capture the back button in the main activity and propagate the event to the current fragment. If the fragment returns true it means it has taken care of the event.
IBackbuttonListener.java
public interface IBackButtonListener {
public boolean onBackButtonPressed();
}
Fragment that implements IBackButtonListener and has a webview loaded from html data.
private int historyStackPointer = 0;
...
#Override
public boolean onBackButtonPressed() {
boolean rtn = false;
if (webView.canGoBack()) {
if(historyStackPointer > 1) {
webView.goBack();
historyStackPointer--;
rtn = true;
}else{
if(historyStackPointer == 1) {
// Reload the html data
webView.loadDataWithBaseURL("file:///android_asset/", html_data, "text/html", "UTF-8", null);
historyStackPointer = 0;
rtn = true;
}else{
webView.loadUrl("about:blank");
rtn = false;
}
}
} else {
rtn = false;
}
return rtn;
}
html_data is a String with the page's html.
What I noticed is that if the url ends in .html, that white screen appears when back button is pressed.
On the other hand, if you remove that .html from your url - obviously only if this is supported by that website (i.e. the redirection and all is handled properly at the server side and that it doesn't trigger the 404 Page Not Found error), that url will act as your base this time and when you press the back button, that white screen should not appear this time.
for example: you have to replace
http://example.com/page.html to:
http://example.com/page

Categories

Resources