Android GeoLocation doesn't work on device - android

I'm writing a basic mobile wrapper that connects to a site mainly using Javascript. This site has a login form that tries to access the geolocation of the device in question. When run on an emulator, there is no prompt to allow location (which makes sense in my mind since the PC has a physical location and an external IP address etc) and the GeoLocation just goes through no problem, which to me suggests that the code is at least functional as far as permissions and webView settings go. When run on a device which has location services enabled, even if I'm prompted and allow location the site cannot resolve my location.
The webView and settings are shown below, and the Android Manifest below that.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wView = (WebView) findViewById(R.id.activity_main_webview);
WebView.setWebContentsDebuggingEnabled(true);
wView.setWebChromeClient(new WebChromeClient(){
#Override
public void onGeolocationPermissionsShowPrompt(final String origin,
final GeolocationPermissions.Callback callback) {
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setMessage( origin + " would like to use your current location" );
builder.setNegativeButton( "Don't Allow", new DialogInterface.OnClickListener() {
#Override
public void onClick( DialogInterface dialog, int which ) {
callback.invoke( origin, false, false );
dialog.dismiss();
}
} );
builder.setPositiveButton( "Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick( DialogInterface dialog, int which ) {
callback.invoke( origin, true, false );
dialog.dismiss();
}
});
}
});
wView.getSettings().setJavaScriptEnabled(true);
wView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wView.getSettings().setGeolocationEnabled(true);
String url = BuildConfig.HOST_URL;
//String postData = "username="+ URLEncoder.encode(user,"UTF-8")+"&password="+URLEncoder.encode(password,"UTF-8");
//Log.e("TEST","postData " + postData);
wView.loadUrl(url);
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.toteireland.app" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="Tote Ireland"
android:theme="#style/AppTheme"
>
<activity
android:name="com.toteireland.app.MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Is that the right place for the permissions?

Related

Cannot activate my custom permission in my code

I want to ask users to agree with the disclaimer that I have provided in my application, and then if they agreed, next time they are using the app, we just re-show them the disclaimer for a few seconds without asking to click on anything and redirect them to the application. The best way I could have come up with it was, using a custom permission and however I did add permission commands to my manifest I could not figure out how to actually activate the permission in my code:
import java.util.jar.Manifest;
public class Language extends Activity {
Button engBut, fraBut;
// public static int j;
public void click (View view){
// j=1;
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
public void fraClick (View view){
// j=2;
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_language);
ImageView logo = (ImageView) findViewById(R.id.logo);
ImageView montfortLogo = (ImageView) findViewById(R.id.montfortLogo);
montfortLogo.setImageResource(R.drawable.k);
logo.setImageResource(R.drawable.j);
engBut = (Button) findViewById(R.id.engBut);
engBut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(Language.this);
builder.setCancelable(true);
builder.setTitle("DISCLAIMER");
builder.setMessage(R.string.result_disclaimer);
builder.setNegativeButton("Get me out of here.", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
builder.setPositiveButton("I agree.", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
intent.setAction("com.techideas4you.pharamacy.MyAction");
intent.addCategory("android.intent.category.DEFAULT");
startActivity(intent);
}
});
builder.show();
}
});
fraBut = (Button) findViewById(R.id.fraBut);
Log.i("Locale", String.valueOf(Locale.getDefault()));
}
}
This was my first page code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="example.ex"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="com.android.vending.BILLING" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<permission android:name="testDisclaimer" android:label="DISCLAIMER"
android:description="#string/result_disclaimer"/>
   
<uses-permission android:name="testDisclaimer"/>
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="#string/app_name">
</activity>
<activity
android:name=".ActivityQuestion"
android:icon="#drawable/home"
android:label="Home"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity
android:name=".ActivityResult"
android:icon="#drawable/home"
android:label="Home"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
<activity android:name=".Language">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter >
<action android:name="com.techideas4you.pharamacy.MyAction"
/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
and this is my manifest if anybody could help me out here I really appreciate it.
You have to add android:protectionLevel="dangerous" to the declaration of your permission else it will be granted automatically and users will not be presented with a dialog although you call requestPermission().
<permission android:name="your.package.name.permission.testDisclaimer" android:label="DISCLAIMER"
android:description="#string/result_disclaimer"
android:protectionLevel="dangerous"/>
<uses-permission android:name="your.package.name.permission.testDisclaimer"/>
In order to have the runtime show the disclaimer, you have to call
requestPermissions(new String[]{"your.package.name.permission.testDisclaimer"}, 42);
The result (whether user agreed) will be transmitted to you in onRequestPermissionsResult()
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults)
{
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 42 && grantResults != null){
Toast.makeText(this, "Permission " + permissions[0] + (grantResults[0] == PERMISSION_GRANTED ? "" : " NOT ") + " granted! ", Toast.LENGTH_LONG).show();
}
}
Please note that by using the permission system, you also have to cope with the fact that users will NOT be shown the permission-request-disguised-as-disclaimer if they
[...] turned down the permission request in the past and chose the
Don't ask again option in the permission request system dialog [...]
(quoted from documentation).
So maybe simply writing a flag to SharedPreferences will do the job just as well.

android place picker or google maps api

i want to see all near places and use place picker for that, but when i start app picker appears and disappears, i cant understand why, help please, thx and sorry for my eng.
If you have some offers how i can see near places without picker, please tell me =)
UPD:
Tried change com.google.android.maps.v2.API_KEY on com.google.android.geo.API_KEY dont help.
MainActivity:
public class MainActivity extends AppCompatActivity {
private Button btPlacesAPI;
private TextView tvPlaceAPI;
private int PLACE_PICKER_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvPlaceAPI = (TextView) findViewById(R.id.tv_place_id);
btPlacesAPI = (Button)findViewById(R.id.bt_ppicker);
btPlacesAPI.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(MainActivity.this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(this,data);
String toastMsg = String.format(
"Place: %s \n" +
"Alamat: %s \n" +
"Latlng %s \n", place.getName(), place.getAddress(), place.getLatLng().latitude+" "+place.getLatLng().longitude);
tvPlaceAPI.setText(toastMsg);
}
}
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityMap"></activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAej54bA3F_PnWkVYX1XfKoEevcfC5RtXM" />
</application>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Server_key" />
Key should be server key instead of android api key
refer this link http://www.androidrey.com/google-places-api-for-android-place-picker/

Trouble in using parse

I am trying to make an app which is using parse library for push notifications.I followed the tutorials from parse.com but its not working.
I am getting error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.robotrackerclient/com.robotrackerclient.MainActivity}:
java.lang.IllegalArgumentException: listener==null
I think the problem is in the code.I am having a main activity which is launching activity of the app.I am putting the parse.initialize() code in the onCreate method of main activity.Is this creating the problem? There is one more error: You must call parse.initialize(..) before using the parse library.
The Main Activity is just a simple class getting the lattitude and longitude of the current location and pushing it.
public class MainActivity extends Activity {
private String imei_code_of_device;
private LocationManager lm;
private LocationListener locationListener;
TelephonyManager mngr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Specify an Activity to handle all pushes by default.
//PushService.setDefaultPushCallback(this, MainActivity.class);
Parse.initialize(this, "APP_KEY", "CLIENT_KEY");
PushService.setDefaultPushCallback(this, MainActivity.class);
mngr=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
imei_code_of_device=mngr.getDeviceId();
lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);
locationListener = new MyLocationListener();
if ( !lm.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
else{
lm.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
10000,
0,
locationListener);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10000,0,locationListener);
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(#SuppressWarnings("unused") final DialogInterface dialog, #SuppressWarnings("unused") final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, #SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
private class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc) {
if (loc != null) {
ParseObject po=new ParseObject("LatLang");
po.put("imei_code",imei_code_of_device);
po.put("latitude",loc.getLatitude());
po.put("longitude",loc.getLongitude());
po.saveInBackground();
Toast.makeText(getBaseContext(),"Location changed : Lat: " + loc.getLatitude() +" Lng: " + loc.getLongitude(),Toast.LENGTH_SHORT).show();
}
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Here is my androidmenifest file:
I completed these steps with help of the tutorials available on parse.com.I included everything which parse.com tutorial said me to include.I am not getting the point of Application.Because I have to parse in the one of the activities only and it is not the first activity to be launched.So I dont want to initialize parse at my application startup.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.robotrackerclient"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<permission android:name="com.robotrackerclient.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.robotrackerclient.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.parse.ParseBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.robotrackerclient" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.ParsePushBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
</application>
</manifest>
You should use your app and client keys instead of default values APP_KEY and CLIENT_KEY in this line:
Parse.initialize(this, "APP_KEY", "CLIENT_KEY");
which you can get after signing up at Parse.
OKay thank you very much for helping I am able to send push to parse.com.The mistake was in the manifest file.I updated the menifestfile with providing android:name=."ParseActivity" under the application tag.Then I defined the initialization in the ParseActivity class.
Now can anybody tell how to get the pushed data over server back on another device using the same set of keys?

Trying to get GPS Location and show it in a TextView

I am very new to Android development. So forgive this repetitive question. I've tried many of the solutions on this but none seem to work quite right for me...They all crash on emulator and on my phone.
I'm working on an App and just want to get my GPS Location and then sets the TextView called 'big' to the location's value.
Heres the code. (I have the import statements and everything in my code..I just didnt copy them over
GPSActivity //the activity that starts. the textview R.id.big does exist
public class GPSActivity extends Activity{
private LocationManager manager = null;
private MyLocationListener listener;
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listener = new MyLocationListener(textView);
textView = (TextView) findViewById(R.id.big);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener(textView);
manager.requestLocationUpdates(LocationManager
.GPS_PROVIDER, 5000, 10,listener);
}
}
I implemented my own LocationListener that changes my TextView...I'm not sure if I'm really allowed to do this....
public class MyLocationListener implements LocationListener
{
TextView textView;
public MyLocationListener(TextView textView)
{
this.textView = textView;
}
#Override
public void onLocationChanged(Location location) {
this.textView.setText(location.toString());
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
}
Lastly my manifest file. I made sure to include the permissions I needed.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mytestapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<activity
android:name="com.mytestapp.GPSActivity"
android:permission="ACCESS_FINE_LOCATION"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The problem occurs in GPSActivity when I call requestLocationUpdates(). The app crashes if I leave this line in the code. If I comment it out, the TextView 'big' displays like I would expect it to.
edit: I've also tried creating a LocationListener as an inner-class like I've seen a lot of other people do on SO...and I had the same result (app crash)
Add
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
to your manifest.xml file.
for a complete support of Geolocation using GPS or WiFi use:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
and for the devices that don´t have support for gps use:
<uses-feature
android:name="android.hardware.location.gps"
android:required="false" />
In addition to the ACCESS_FINE_LOCATION permission try adding
ACCESS_GPS
and they should look like this ...
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Keep Android application active on screen timeout

I'm fairly new to making Android application so I got a question.
If I got my application running and I wait for a few seconds my screen timeouts. The problem is that when I unlock my screen again my application is closed. I want the application to stay active when the screen times out, so that I don't have to start the application all over again.
I don't want the application to run in the background and also I do not want to disable the screen timeout. I just don't want the application to close itself after the screen automatically times out.
Is there any chance of doing so?
_EDIT > Added some of my code.
 
Android Manifest
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:resizeable="true"
android:anyDensity="true"
/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".SIOM_AndroidActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:theme="#android:style/Theme.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.phonegap.DroidGap"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden" >
<intent-filter>
</intent-filter>
</activity>
</application>
</manifest>
 
Android Activity
package com.siom.android;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.phonegap.*;
public class SIOM_AndroidActivity extends DroidGap {
/** Called when the activity is first created. */
/** #Override */
WebView mWebView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
super.clearCache();
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setWebViewClient(new HelloWebViewClient());
mWebView.setWebChromeClient(new MyWebChromeClient());
mWebView.loadUrl("http://www.example.com/test.html");
}
// Maakt een WebChromeClient class aan
final Context myApp = this;
final class MyWebChromeClient extends WebChromeClient {
// Zorgt er voor dat confirm werkt in WebView
#Override
public boolean onJsConfirm(WebView view, String url, String message, final JsResult result) {
new AlertDialog.Builder(myApp)
.setTitle("Title here")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.confirm();
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.cancel();
}
})
.create()
.show();
return true;
}
// Zorgt er voor dat alert werkt in WebView
#Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
new AlertDialog.Builder(myApp)
.setTitle("Title here")
.setMessage(message)
.setPositiveButton(android.R.string.ok,
new AlertDialog.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
result.confirm();
}
})
.setCancelable(false)
.create()
.show();
return true;
}
}
// Alle links worden in webview geladen
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
// Geeft de back knop een goback actie
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
if (keyCode == KeyEvent.KEYCODE_MENU) {
return true;
}
return super.onKeyDown(keyCode, event);
}
}
There is no direct answer to ur question. Have a look at Figure 1. The activity lifecycle
The concept says that when screen is locked your activity is no longer visible (onPause()). When screen is turned back on onResume() is called. You can use these methods to get the desired results.
Generally, I initialize everything onCreate() of the activity and refresh the values if any onResume().
Hope that helps.
After so many hours, days, weeks and even months trying to solve this problem I finally found it.
If you look at the code I added in my first post you can see the follow two lines of code:
super.init();
super.clearCache();
These two little guys crashed my application over and over and now after I look at it it's no wonder they did. Added these because I was first working with PhoneGap and followed some tutorial somewhere that told me to add these lines. After removing these two lines again my application worked as usual again. Only removing one line didn't work either.
Sometimes programming can really be a hell.

Categories

Resources