【问题标题】:Android Check for Wireless connection upon Returning to Activity from SettingsAndroid 从设置返回活动时检查无线连接
【发布时间】:2014-04-17 13:43:40
【问题描述】:

我的 Android 应用程序主要是一个浏览移动网站的 Webview(标签为 WebViewActivity)。如果没有无线连接,我有一个连接检测器,它会在每个 URL 加载时打开另一个活动。在下一个活动中,有一个按钮可以打开无线设置。按下后退按钮后,我希望第二个活动(标记为 MainActivity)自行刷新并继续返回到我的 Webview 加载的任何页面。我需要在 MainActivity.java 中更改什么来执行它?

我的主要活动:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;


    public class MainActivity extends Activity {

// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

 }

@Override
protected void onRestart(){
    if(checkConnection()){
        Intent intent= new Intent(this, WebViewActivity.class);
        startActivity(intent);
    }
}

@Override
// Detect when the back button is pressed
public void onBackPressed() {

    super.onBackPressed();
}




public void openSettings(View view){
    Intent intent= new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
    startActivity(intent);
}

public boolean checkConnection(){
    // creating connection detector class instance
    cd = new ConnectionDetector(getApplicationContext());       
    //Get Internet Status
    isInternetPresent = cd.isConnectingToInternet();

    if(!isInternetPresent)
        return false;

    return true;
}

}

我的 WebviewActivity:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebViewActivity extends Activity {

    private WebView mWebView;
    // flag for Internet connection status
    Boolean isInternetPresent = false;
    // Connection detector class
    ConnectionDetector cd;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // TODO Auto-generated method stub

        if (checkConnection()) {
        mWebView = (WebView) findViewById(R.id.activity_main_webview);
         // Enable Javascript
         WebSettings webSettings = mWebView.getSettings();
         webSettings.setJavaScriptEnabled(true);         
         webSettings.setDomStorageEnabled(true);
         mWebView.loadUrl("http://my.fellowshipnwa.org/?publicapp");

        // Force links and redirects to open in the WebView instead of in a browser
         mWebView.setWebViewClient(new myWebClient());
        } else {
            openSplash();
        }

    }

    @Override
    // Detect when the back button is pressed
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            // Let the system handle the back button
            new AlertDialog.Builder(this)
            .setTitle("Exit myFellowship App?")
            .setMessage("Are you sure you want to exit?")
            .setNegativeButton(android.R.string.no, null)
            .setPositiveButton(android.R.string.yes, new OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                    WebViewActivity.super.onBackPressed();
                }
            }).create().show();
        }
    }

    public class myWebClient extends WebViewClient {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if(!checkConnection()){
                openSplash();
                return true;
            }else{
            if( url.startsWith("tel:")){
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));   
                startActivity(intent);
                return true;
            }
            else if( url.startsWith("mailto:")){
                Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse(url));
                startActivity(intent);
                return true;
            }
            }
            return false;

            }


        } 

    public void openSplash(){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    public boolean checkConnection(){
        // creating connection detector class instance
        cd = new ConnectionDetector(getApplicationContext());       
        //Get Internet Status
        isInternetPresent = cd.isConnectingToInternet();

        if(!isInternetPresent)
            return false;

        return true;
    }

}

非常感谢任何帮助!即使这意味着告诉我我在创建这个问题时搞砸了。

谢谢朋友们。

【问题讨论】:

    标签: android-activity settings wireless


    【解决方案1】:

    我玩了一段时间,并使用了一些其他示例来拼凑一些有效的东西。一个小问题是,如果用户打开了一个连接,但当他们返回应用程序时它并没有完全连接,它将停留在 ConnectorActivity 上。所以我拦截了后退按钮以检查连接。如果存在,它将完成当前活动。然后,在返回到 webview 时,它将重新加载。

    ConnectorActivity.java:

    @Override
        protected void onRestart(){
            super.onRestart();
            if(checkConnection()){
                finish();
            }
        }
    
        @Override
        // Detect when the back button is pressed
        public void onBackPressed() {
            if(checkConnection()){
                super.onBackPressed();
            }
    
        }
    

    来自 WebViewActivity.java 的片段:

    @Override
    protected void onRestart(){
        super.onRestart();
        mWebView.reload();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多