IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

API standards et tierces Android Discussion :

vérification connexion disponible sur un webView


Sujet :

API standards et tierces Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2013
    Messages
    100
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2013
    Messages : 100
    Par défaut vérification connexion disponible sur un webView
    bonsoir a tous, j'ai une application développée en web et incorporées dans un webView, j'aimerais avoir
    le code java permettant de vérifier ma connexion ou la disponibilité de la connexion avant de charger une page.
    Merci pour une prompte réaction de votre part.

  2. #2
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2017
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Congo-Kinshasa

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2017
    Messages : 11
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    private void checkConnectivity(){
            ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
            if(cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnectedOrConnecting() ||
                    cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting()){
                online=true;
            }else{
                online=false;
            }
        }
    N'oublie ces permissions :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <uses-permission android:name="android.permission.INTERNET"/>
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
        <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

  3. #3
    Membre habitué
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2017
    Messages
    11
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Congo-Kinshasa

    Informations professionnelles :
    Activité : Étudiant
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2017
    Messages : 11
    Par défaut
    Petite précision, il faudra faire cette vérification avant chaque chargement de la page par le webview

  4. #4
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2013
    Messages
    100
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2013
    Messages : 100
    Par défaut
    bonsoir, Merci pour votre prompte réaction, je teste et je vous reviens

  5. #5
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2013
    Messages
    100
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2013
    Messages : 100
    Par défaut
    bonsoir, Danus14, après avoir testé ce code, il ne répond pas à mon problème, puisque lorsque je désactive ma connection et je je charge mon webView avec le page web d’accueil, il ne me signale pas l'absence de connexion et me renvoie ma page web avec le message par défaut d'android et le lien de ma page que je ne veut pas voir. j'ai meme remplacé la methode isconnectedOrConnecting par isAvailable, jusque là rien.
    voici mon code complet:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
     
    package com.tnt.driver;
     
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.Toast;
    import android.net.ConnectivityManager;
    import android.net.Uri;
    public class MainActivitygo extends Activity {
    	private Context mContext;
    	@SuppressLint("SetJavaScriptEnabled")
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_go);
    		 mContext = getApplicationContext();
    	String url="http://tnt.com/driver/fr/";
     
    		WebView wv=(WebView)findViewById(R.id.webView1);
    	      wv.setWebViewClient(new WebViewClient());
    	     wv.getSettings().getJavaScriptEnabled();
    	      wv.getSettings().setJavaScriptEnabled(true);
     
              wv.getSettings().setLoadsImagesAutomatically(true);
              wv.addJavascriptInterface(url, ACCESSIBILITY_SERVICE);
              wv.canGoBackOrForward(0);
              wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
     
     
    		  ConnectivityManager cm=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    	        @SuppressWarnings("unused")
    			boolean online;
     
    	        if(cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isAvailable() ||
    	                cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isAvailable()){
     
    	        	wv.loadUrl(url);
     
    	        	Toast.makeText(getApplicationContext(), "Verification connexion internet", Toast.LENGTH_LONG).show();
     
     
    		  // Set the web view client
    	        wv.setWebViewClient(new WebViewClient(){
     
    	            // For  level bellow 24
    	            @Override
    	            public boolean shouldOverrideUrlLoading(WebView view, String url){
    	                Toast.makeText(mContext, "GetDrivers",Toast.LENGTH_SHORT).show();
     
     
     
    	                if(url.startsWith("http")){
    	                    Toast.makeText(mContext,"patientez...",Toast.LENGTH_SHORT).show();
    	                    // Return false means, web view will handle the link
    	                    return false;
    	                }else if(url.startsWith("tel:")){
    	                    // Handle the  link
    	                    handleTelLink1(url);
     
    	                    // Return true means, leave the current web view and handle the  itself
    	                    return true;
    	                }
     
    	                return false;
    	            }
     
     
    	            // From  level 24
     
    	        });
    	        wv.loadUrl(url);
     
     
    			 }
    	        else{
    				 Toast.makeText(getApplicationContext(), "GetDrivers n'a pas de connexion", Toast.LENGTH_LONG).show();
     
    		     }
    	}
     
     
     
     
    	protected void handleTelLink1(String url) {
    		// TODO Auto-generated method stub
    		 Intent intent = new Intent(Intent.ACTION_DIAL);
     
    	        // Send phone number to intent as data
    	        intent.setData(Uri.parse(url));
     
     
    	        startActivity(intent);
    	}
     
     
     
     
     
    	@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);
    	}
     
    	  protected void handleTelLink(String url){
              // Initialize an intent to open dialer  with specified phone number
              // It open the dialer  and allow user to call the number manually
              Intent intent = new Intent(Intent.ACTION_DIAL);
     
              // Send phone number to intent as data
              intent.setData(Uri.parse(url));
     
              // Start the dialer  activity with number
              startActivity(intent);
          }
    }

  6. #6
    Membre confirmé
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2013
    Messages
    100
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Septembre 2013
    Messages : 100
    Par défaut
    A force de chercher sans trouver, on finit par trouver sans chercher
    voici ce fameux code que je cherchais pour intercepter l'absence de connexion avant de charger ma page web dans un webView ceci pourra aider les autres
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
     wv.setWebViewClient(new WebViewClient() {
    		 public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    			 wv.loadDataWithBaseURL("file:///android_asset/www/error.html", "Désolé! Vous n'avez pas de Connexion internet pour continuer", "text/html", "utf-8", null); } 
    		 });
    	 }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Comment etablir une connexion distante sur une bd sql server
    Par sessime dans le forum Bases de données
    Réponses: 2
    Dernier message: 08/11/2004, 13h19
  2. Problème de Connexion à Pg sur Windows 98
    Par bubble_gum dans le forum PostgreSQL
    Réponses: 3
    Dernier message: 24/07/2004, 19h44
  3. [VB.NET] Connexion distante sur serveur UNIX
    Par Toftof dans le forum Windows Forms
    Réponses: 5
    Dernier message: 30/06/2004, 17h18
  4. tracer les connexions web sur un serveur
    Par lutin511 dans le forum Développement
    Réponses: 2
    Dernier message: 28/06/2004, 23h36
  5. Connexion SYSDBA sur une base ou une autre
    Par jack554 dans le forum Administration
    Réponses: 11
    Dernier message: 20/04/2004, 17h11

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo