Android - WebView i pobieranie danych w tle


(northwest) #1

Witam serdecznie,

Mam aplikację webview uruchomioną na pełnym ekranie i chciałbym w oddzielnym wątku (tam żeby nie wyskakiwał błąd mówiązcy iż aplikacja nie odpowiada) uruchomić POST'a do skryptu PHP.

Aktualnie mam taki kod:

MenuActivity.java

package butterfly.menu;


import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.content.res.Configuration;

import android.os.Bundle;

import android.view.KeyEvent;

import android.webkit.WebView;

import android.webkit.WebViewClient;


public class MenuActivity extends Activity {

	WebView mWebView;

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) { 

    	super.onCreate(savedInstanceState);


    	startService(new Intent(this, OnAlarmReceiver.class));


    	setContentView(R.layout.main);

        if (savedInstanceState != null)

            ((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState);


        mWebView = (WebView) findViewById(R.id.webview);

        mWebView.getSettings().setJavaScriptEnabled(true); // włączenie JS

        mWebView.getSettings().setSupportZoom(false); // blokada ZOOMu




        //mWebView.loadUrl("http://topvesta.home.pl/tablety"); // strona do wyświetlenia

        mWebView.setWebViewClient(new MenuClient());

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)

        {

            mWebView.loadUrl("http://topvesta.home.pl/tablety");

        }


        else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)

        {

            mWebView.loadUrl("http://topvesta.home.pl/tablety");

        }

    }

    private class MenuClient extends WebViewClient {

        @Override

        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            view.loadUrl(url);

            return true;

        }

    }

    protected void onSaveInstanceState(Bundle outState) {

    	mWebView.saveState(outState);

     }


    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if ((keyCode == KeyEvent.KEYCODE_HOME || keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_VOLUME_DOWN || keyCode == KeyEvent.KEYCODE_VOLUME_UP || keyCode == KeyEvent.KEYCODE_CALL || keyCode == KeyEvent.KEYCODE_POWER || keyCode == KeyEvent.KEYCODE_CAMERA || keyCode == KeyEvent.KEYCODE_CLEAR || keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_POWER)) {

            //mWebView.goBack();

            return false;

        }

        return super.onKeyDown(keyCode, event);

    }


    @Override

	public void onConfigurationChanged(Configuration newConfig) {

    	if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)

        {

            mWebView.loadUrl("http://topvesta.home.pl/tablety");

        }


        else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)

        {

            mWebView.loadUrl("http://topvesta.home.pl/tablety");

        }

		super.onConfigurationChanged(newConfig);

	} 

    public void onReceive(Context context, Intent intent) { 

    	startService(new Intent(this, OnAlarmReceiver.class));

    	super.onReceive(Context context, Intent intent);

    } 


}

oraz OnAlarmReciver.java

package butterfly.menu;

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;


import org.apache.http.HttpResponse;

import org.apache.http.NameValuePair;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.message.BasicNameValuePair;


import android.app.Service;

import android.content.Intent;

import android.os.IBinder;


public class OnAlarmReceiver extends Service {

	@Override

	public IBinder onBind(Intent intent) {

		return null;

	}


	@Override

	public void onCreate() {

		HttpClient httpclient = new DefaultHttpClient();

	    HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");


	    try {

	        // Add your data

	        List nameValuePairs = new ArrayList(2);

	        nameValuePairs.add(new BasicNameValuePair("id", "12345"));

	        nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));

	        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


	        // Execute HTTP Post Request

	        HttpResponse response = httpclient.execute(httppost);


	    } catch (ClientProtocolException e) {

	        // TODO Auto-generated catch block

	    } catch (IOException e) {

	        // TODO Auto-generated catch block

	    }

        }

}

Ale nie wiem czy dobrze to robię :frowning: coś wywołanie tego wątku nie chce mi za bardzo działać... :frowning: Wiecie może dlaczego?

Z góry dziękuje za pomoc,

Northwest