0

我的应用程序有一个 3 选项卡式 ActionBar 布局。这 3 个选项卡是仪表板提要消息

当您单击这三个中的任何一个时,应用程序应该创建一个 www.flyalaskava.org/incog/mobile/ 的 WebView - 如果您没有活动会话 - 将显示一个图像和一个“登录脸书”按钮。

问题是,当我加载第一个选项卡 ( Dashboard ) 并使用 Facebook cliek 登录时,它会让我登录 - 但是一旦我点击另一个选项卡,我就会失去我的会话并被重新提示。

请记住,目前所有这些都使用相同的php文件,并且登录系统在 Android 之外可以完美运行。对不起,如果这是一个新手问题 - 任何帮助表示赞赏。

package com.example.testing;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;

import com.handmark.pulltorefresh.library.PullToRefreshWebView;

public class Main extends FragmentActivity implements ActionBar.TabListener {

    PullToRefreshWebView mPullRefreshWebView;
    WebView mWebView;

    /**
     * The serialization (saved instance state) Bundle key representing the
     * current tab position.
     */
    private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";

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

        mPullRefreshWebView = (PullToRefreshWebView) findViewById(R.id.pull_refresh_webview);
        mWebView = mPullRefreshWebView.getRefreshableView();

        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new SampleWebViewClient());
        mWebView.loadUrl("http://www.google.com");

        // Set up the action bar to show tabs.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // For each of the sections in the app, add a tab to the action bar.
        actionBar.addTab(actionBar.newTab().setText(R.string.title_section1)
            .setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.title_section2)
            .setTabListener(this));
        actionBar.addTab(actionBar.newTab().setText(R.string.title_section3)
            .setTabListener(this));
    }

    private static class SampleWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        // Restore the previously serialized current tab position.
        if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) {
            getActionBar().setSelectedNavigationItem(
                savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)
            );
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        // Serialize the current tab position.
        outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar()
            .getSelectedNavigationIndex());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_settings:
                displayAlert();
                break;
            case R.id.menu_exit:
                displayExit();
                break;
            default:; 
        }
        return(super.onOptionsItemSelected(item));
    }

    public  void displayAlert() {
        new AlertDialog.Builder(this)
            .setMessage("This Application was created by Grant Adkins")
            .setTitle("About")  
            .setCancelable(false)  
            .setNeutralButton(android.R.string.ok,  
                new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int whichButton){
                        dialog.cancel();
                    }  
                }
            )  
            .show(); 
    }

    public  void displayExit() {
        new AlertDialog.Builder(this).setMessage("Exit the application")  
            .setTitle("Are you sure?")  
            .setCancelable(false)  
            .setNeutralButton(android.R.string.no,  
                new DialogInterface.OnClickListener() {  
                    public void onClick(DialogInterface dialog, int whichButton){
                        dialog.cancel();
                    }  
                }).setPositiveButton(android.R.string.yes,  
                    new DialogInterface.OnClickListener() {  
                        public void onClick(DialogInterface dialog, int whichButton){
                            finish();
                        }  
                    }
                ) 
            .show(); 
    }
    public boolean isOnline() {
        ConnectivityManager cm = 
            (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        String summary = "<html><body>No Network Connection.</body></html>";
        mWebView.loadData(summary, "text/html", null);
        return false;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, show the tab contents in the
        // container view
        int page = tab.getPosition() + 1;
        if(page == 1) {
            /// eventually is going to load index.php?content=dashboard
            mWebView.loadUrl("http://www.flyalaskava.org/incog/mobile/");
            isOnline();
        } else if (page == 2) {
            /// eventually is going to load index.php?content=messages
            mWebView.loadUrl("http://www.flyalaskava.org/incog/mobile/");
            isOnline();
        } else if (page == 3) {
            /// eventually is going to load index.php?content=feed
            mWebView.loadUrl("http://www.flyalaskava.org/incog/mobile/");
            isOnline();
        } else {
            mWebView.loadUrl("http://www.google.com");
            isOnline();
        }
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            // Create a new TextView and set its text to the fragment's section
            // number argument value.
            TextView textView = new TextView(getActivity());
            textView.setGravity(Gravity.CENTER);
            textView.setText(
                Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER))
            );

            return textView;
        }
    }
}`

*\\\\\\\\更新\\\\\\*

我发现这篇文章似乎是一个类似的问题,可能是因为我使用mWebView.loadUrl("http://www.flyalaskava.org/incog/mobile/"); 它就像一个新的浏览器,有没有办法在不使用该方法的情况下更改 url。

是问题的图片。

4

2 回答 2

0

考虑使用服务而不是 cookie。服务将允许您在应用程序的后台保持与服务器的连接。服务一旦创建就很容易被引用(阅读服务和意图)。然后,该服务将充当您与服务器的会话。

于 2012-12-08T00:22:58.587 回答
0

创建您的后添加以下行WebView

CookieSyncManager.createInstance(mWebView.getContext()).sync();
CookieManager.getInstance().acceptCookie();
于 2012-12-08T00:31:38.940 回答