take part of an Android Studio string

1

I'm trying to take part of some SMS in an android application, the idea is that the sms just take the link, but the problem is that sometimes it can contain more or less characters and be located in different parts of the message for which I can not use substring or indexOf (according to what I tried). The only thing that is constant is that it starts in link and ends in z = 16 but I do not know how to start it in http I take the whole string until I get to z = 16 included. Will it be?

An example of a message would be something like this

1st sms

ACC alarm!     lat: -31.424482

long:-64.160276

speed:0.04 

T:18/10/31 14:39

http://maps.google.com/maps?f=q&q=-31.424482,-64.160276&z=16

2nd SMS

Door alarm!

lat:-31.424154

long:-64.162243

speed:0.61 

http://maps.google.com/maps?f=q&q=-31.424154,-64.162243&z=16

T:18/10/31 14:53

My code so far.

  @Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    Log.e("PERMISOS","ingresa onCreateView");
 //   return inflater.inflate(R.layout.fragment_bentrada,container,false);
    View v = inflater.inflate(R.layout.fragment_bentrada, container, false);

    Cursor cursor = getActivity().getContentResolver().query(Uri.parse(INBOX), null, null, null, null);

    String enlace = "";

    if (cursor.moveToFirst()) {
        while ( cursor.moveToNext() && cantmensaje ==1){//recorro todos los SMS y en caso de encotrar el SMS buscado cambio la variable
            String cuerpo = cursor.getString(cursor.getColumnIndex("body"));
            String longitud ="";
            String longituds ="";
            String latitud = "";
            String latituds = "";
            char [] caracteres = cuerpo.toCharArray();


       /*    for(int i= cuerpo.indexOf("http:"); i>0; i--){ //empieza el analisis desde la palabra años hacia atras


                if(Character.isDigit(caracteres[i]) &&  Character.toLowerCase(caracteres[i]) != ','){ //Revisando que realmente el caracter seleccionado sea un numero
                    latitud = String.valueOf(String.valueOf(caracteres, i-1, 11).trim()); //toma el numero de la posicion actual y el anterior pero si este es un espacio en blanco lo elimina

                        latituds = latitud.trim();

                }

                if(Character.isDigit(caracteres[i]) ){ //Revisando que realmente el caracter seleccionado sea un numero
                    longitud = String.valueOf(String.valueOf(caracteres, (i+latitud.length()) + 5, 11).trim()); //toma el numero de la posicion actual y el anterior pero si este es un espacio en blanco lo elimina


                        longituds = longitud.replace(",", "").trim();

                        longituds.trim();


                }

            }*/

            Log.e("","Tiene longitud "+longituds);

            Log.e("","Tiene Latitud "+latituds);

                if (latituds != null && longituds != null){
                    final WebView webView = (WebView) v.findViewById(R.id.googlemaps_webview);
                    webView.getSettings().setJavaScriptEnabled(true);
                    webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
                    webView.setWebViewClient(new WebViewClient());
                    //webView.loadUrl("http://google.com");
                    webView.loadUrl("http://maps.google.com/maps?f=q&q="+latituds+","+longituds+"&z=16");
                    //webView.setWebViewClient(new WebViewClient() {
                    //    @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                    //    webView.loadUrl("file:///android_asset/erroryaviene.html");
                    //          } });
                    Log.e("ya viene","http://maps.google.com/maps?f=q&q="+latitud+","+longituds+"&z=16");
                    Log.e("ya viene",""+cuerpo);
                    // }
                    Log.e("PERMISOS","ingresa al mapa");
                    // cantmensaje = cantmensaje + 1;
                }

        }
    } else {
        //No hay SMS
        Log.e("PERMISOS","no ingreso a view ");
    }

    return v;

}
    
asked by Luis 31.10.2018 в 19:55
source

1 answer

1

Java is not my natural, but there are two ways you can solve it, depending on your argument input:

1) If your text is multiline, you could save it as an array where each line is an element of it and read position [3], assuming it is always there. If you change the line you should go through the array and verify the position that you start with "http".

2) If your text is not multiline, you can apply this option that I just did: link The logic is that you know the start and end phrase of the substring you want to search, so you can use the indexOf (String s) class to get your flag index among which is what to look for, and pass them in the Substring class (int, int) to delimit where to find your string.

    
answered by 31.10.2018 / 20:55
source