У меня есть приложение веб-просмотра.Мне нужно добавить в эту ветку программы, которые публикуют мой PHP-скрипт (работает в фоновом режиме).У меня есть этот код: Файл: MenuActivity 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://onet.home.pl/"); // strona do wyświetlenia
mWebView.setWebViewClient(new MenuClient());
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
mWebView.loadUrl("http://onet.home.pl/");
}
else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
mWebView.loadUrl("http://onet.home.pl/");
}
}
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://onet.home.pl/");
}
else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
{
mWebView.loadUrl("http://onet.home.pl/onet");
}
super.onConfigurationChanged(newConfig);
}
public void onReceive(Context context, Intent intent) {
startService(new Intent(this, OnAlarmReceiver.class));
super.onReceive(Context context, Intent intent);
}
}
и OnAlarmReciver:
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<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(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
}
}
}
И он не работает :( Тема не запущена: (
Кто сейчас, что у меня плохо?
Спасибо за помощь