私はEclipseとJavaにまったく慣れていないということから始めるべきだと思います。Web ページを起動する Eclipse を使用して Android アプリを作成しようとしています。私は問題なく動作するコードの例を持っていますが、オンラインで見つけた例からほとんどコピーして貼り付けているので、ずさんなコードを許してください。インターネットまたは Wi-Fi 接続が利用可能かどうかを確認する方法を知りたい.. 接続がない場合はアラートを表示する (インターネット接続なし).. 同様の質問をいくつか見つけましたが、どこに配置すればよいかわかりません。コード?誰か見せてくれませんか?
package com.mysite.news;
import com.mysite.news.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class WebActivity extends Activity {
private WebView wv;
private String LASTURL = "";
Menu myMenu = null;
private static final String PREFS_NAME = "MyPrefs";
private Boolean imgOn;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
this.getWindow().requestFeature( Window.FEATURE_PROGRESS );
setContentView( R.layout.web_view );
wv = ( WebView ) findViewById( R.id.web_view );
WebSettings webSettings = wv.getSettings();
webSettings.setSavePassword( true );
webSettings.setSaveFormData( true );
webSettings.setJavaScriptEnabled( true );
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(false);
SharedPreferences settings = getSharedPreferences( PREFS_NAME, 0 );
imgOn = settings.getBoolean( "IMGMODE", false );
webSettings.setLoadsImagesAutomatically( imgOn );
final Activity activity = this;
// start ProgressDialog with "Page loading..."
final ProgressDialog dialog = new ProgressDialog( activity );
dialog.setMessage( "Page loading..." );
dialog.setIndeterminate( true );
dialog.setCancelable( true );
dialog.show();
wv.setWebChromeClient( new WebChromeClient() {
public void onProgressChanged( WebView view, int progress ) {
// set address bar and progress
// activity.setTitle( " " + LASTURL );
// activity.setProgress( progress * 100 );
if( progress == 100 ) {
// stop ProgressDialog after loading
dialog.dismiss();
// activity.setTitle( " " + LASTURL );
}
}
} );
wv.setWebViewClient( new WebViewClient() {
public void onReceivedError( WebView view, int errorCode, String description, String failingUrl ) {
Toast.makeText( getApplicationContext(), "Error: " + description + " " + failingUrl, Toast.LENGTH_LONG ).show();
}
@Override
public boolean shouldOverrideUrlLoading( WebView view, String url ) {
if( url.indexOf( "mysite" ) <= 0 ) {
// the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent( Intent.ACTION_VIEW, Uri.parse( url ) );
startActivity( intent );
return true;
}
return false;
}
public void onPageStarted( WebView view, String url, Bitmap favicon ) {
LASTURL = url;
view.getSettings().setLoadsImagesAutomatically( true );
view.getSettings().setBuiltInZoomControls( true );
}
public void onPageFinished( WebView view, String url ) {
view.loadUrl( "javascript:(function() { " +
"hide('sidebar');" +
//"var parent = document.getElementsByClassName('page-navigation')[0];"+
//"var panel = document.getElementsByClassName('panel-tools')[0];"+
//"var div = document.createElement('div');"+
//"div.innerHTML = panel.innerHTML;"+
//"parent.appendChild(div);"+
//"panel.innerHTML = '';"+
//"div.style['margin-left'] = '31px';"+
"var panel = document.getElementById('search');" +
"panel.style['width'] = '55px';" +
//"var imgs=document.getElementsByTagName('IMG');for(var i=0;i<imgs.length;i++){if (imgs[i].height=60) {imgs[i].src='';imgs[i].width=0;} }"+
//"var urls=document.getElementsByTagName('li');for(var i=0;i<urls.length;i++){if (urls[i].style='margin: -14px 0pt 0pt;'){urls[i].style['display'] = 'none';}}"+
//"hideByClass('panel-tools');"+
"function hide(id){if (document.getElementById(id)){document.getElementById(id).style['display'] = 'none';}}" +
//"function hideByClass(c){var e=document.getElementsByClassName(c);for(var i=0;i<e.length;i++){e[i].style['display'] = 'none';}}"+
"})()" );
if( imgOn ) {
view.getSettings().setLoadsImagesAutomatically( true );
view.getSettings().setSupportZoom(false);
}
}
} );
wv.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
wv.setScrollbarFadingEnabled(false);
wv.loadUrl( "http://www.mysite.com" );
}
@Override
public boolean onKeyDown( int keyCode, KeyEvent event ) {
if( ( keyCode == KeyEvent.KEYCODE_BACK ) && wv.canGoBack() ) {
wv.goBack();
return true;
}
return super.onKeyDown( keyCode, event );
}
@Override
public boolean onCreateOptionsMenu( Menu menu ) {
super.onCreateOptionsMenu( menu );
this.myMenu = menu;
MenuItem item = menu.add( 0, 1, 0, "MAIN PAGE" );
item.setIcon( R.drawable.home );
MenuItem item2 = menu.add( 0, 2, 0, "BACK" );
item2.setIcon( R.drawable.arrowleft );
MenuItem item3 = menu.add( 0, 3, 0, "Reload" );
item3.setIcon( R.drawable.s );
MenuItem item4 = menu.add( 0, 4, 0, "CLEAR CACHE" );
item4.setIcon( R.drawable.trash );
MenuItem item5 = menu.add( 0, 5, 0, "Rate" );
item5.setIcon( R.drawable.vote );
MenuItem item6 = menu.add( 0, 6, 0, "Exit" );
item6.setIcon( R.drawable.close );
return true;
}
@Override
public boolean onOptionsItemSelected( MenuItem item ) {
switch( item.getItemId() ) {
case 1:
wv.loadUrl( "http://mysite.com" );
break;
case 2:
if( wv.canGoBack() ) {
wv.goBack();
}
break;
case 3:
wv.loadUrl( LASTURL );
break;
case 4:
wv.clearCache( true );
break;
case 5:
Intent marketIntent2 = new Intent(Intent.ACTION_VIEW, Uri.parse(
"https://play.google.com/store/apps/details?id=" + getPackageName()));
startActivity(marketIntent2);
break;
case 6:
finish();
break;
}
return true;
}
private void saveSettings( Boolean val ) {
SharedPreferences settings = getSharedPreferences( PREFS_NAME, 0 );
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean( "IMGMODE", val );
editor.commit();
}
}