如何在首次登录简单的Webview应用程序android后自动登录用户

时间:2018-03-14 19:29:19

标签: android cookies webview android-webview

起初我想感谢您的关注。

我有一个简单的网页浏览(客户端)应用程序,我想设置一个可以在用户首次登录后自动登录用户的选项...
这意味着当用户关闭应用程序并重新打开它已保持登录时 我已经检查了堆栈中的许多主题......但我只是理解使用cookie但不知道如何使用...

public class MainActivity extends AppCompatActivity {

    private WebView webView;
    FloatingActionButton fab;

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



        fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                webView.loadUrl("http://www.datalifeengine.ir/");
                Toast.makeText(MainActivity.this, "homepage...", Toast.LENGTH_SHORT).show();
            }
        });


        String url;
        Intent intent = getIntent();
        String action = intent.getAction();
        Uri data = intent.getData();


        if (data != null) {
            webView.loadUrl(data.getScheme() + "://" + data.getHost() + data.getPath());
        } else {
            webView.loadUrl("http://www.datalifeengine.ir/");
        }

    }


    @Override
    public void onBackPressed() {

        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    public void navcolor() {
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setNavigationBarColor(ContextCompat.getColor(this, R.color.colorPrimaryDark));
        }
    }

    public void webclient() {
        webView = (WebView) findViewById(R.id.webview);
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setLoadWithOverviewMode(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.getSettings().setBuiltInZoomControls(true);

    }

    void check() {
        ConnectivityManager conMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        if (conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED
                || conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {

            Toast.makeText(this, "online", Toast.LENGTH_SHORT).show();
        } else if (conMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.DISCONNECTED
                || conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.DISCONNECTED) {

            Toast.makeText(this, "offline", Toast.LENGTH_SHORT).show();
        }
    }

    public void onesignal(){
        OneSignal.startInit(this)
                .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
                .unsubscribeWhenNotificationsAreDisabled(true)
                .init();

    }


}

0 个答案:

没有答案