gpt4 book ai didi

android - 如何在 webview 和 chrome 中同步 cookie android

转载 作者:行者123 更新时间:2023-11-29 21:13:52 25 4
gpt4 key购买 nike

我已经搜索过这个但直到现在才在 stackoverflow 中找到答案。我的项目是创建 android webview 应用程序显示网站的移动版本。我知道网站无法检测到在 chrome 浏览器中运行时是否安装了应用程序以确保安全。

所以我用附加应用程序的名称更改了 android 应用程序的用户代理。然后每次用户使用该应用程序时,网站都会检测到网络正在 webview 中运行,然后在设备中写入 cookes“Appname”。

如果用户使用 chrome 浏览网站会从那里找到该 cookie,如果没有找到,web 将显示一些帮助用户安装应用程序的链接。

问题是我的应用程序和 chrome 之间的 sysn cookie 看起来不起作用。

以下是我的activity.java感谢您的帮助!

public class XaLuanActivity extends Activity{   
private static final String tag=XaLuanActivity.class.getCanonicalName();
private static final int CHIASE_MENU=1;
private static final int THOAT_MENU=2;
private static final int TAILAI_MENU=3;
private static final int TROGIUP_MENU=4;
private static final int DANHGIA_MENU=5;

private String mainUrl="http://www.xaluan.com/m.php";
ProgressBar mProgressBar;
WebView wbvContent;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
CookieSyncManager.createInstance(this);
CookieSyncManager.getInstance().startSync();
CookieManager.getInstance().setAcceptCookie(true);
setContentView(R.layout.xaluan);
wbvContent=(WebView) findViewById(R.id.wbvContent);
//wbvContent.setBackgroundColor(Color.parseColor("#FFFFFF"));
mProgressBar=(ProgressBar)findViewById(R.id.progressBar);
wbvContent.getSettings().setJavaScriptEnabled(true);
wbvContent.setInitialScale(1);
wbvContent.getSettings().setLoadWithOverviewMode(true);
wbvContent.getSettings().setUseWideViewPort(true);
wbvContent.getSettings().setSupportZoom(true);
wbvContent.getSettings().setBuiltInZoomControls(true);

wbvContent.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
wbvContent.getSettings().setUserAgentString(
wbvContent.getSettings().getUserAgentString()
+ " "
+ getString(R.string.user_agent_suffix)
); // detect useragent and add XLAndroidApp suffix name to the end

HelloWebViewClient wbvClient=new HelloWebViewClient();
wbvContent.setWebViewClient(wbvClient);

if(!isConnectedNetwork(this)) {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Thông báo")
.setMessage("Không có kết nối mạng, vui lòng kết nối mạng để sử dụng")
.setPositiveButton("Thử lại", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
XaLuanActivity.this.finish();
startActivity(new Intent(XaLuanActivity.this, XaLuanActivity.class));
}
})
.setNegativeButton("Thoát", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
XaLuanActivity.this.finish();
}
})
.show();
}else {
wbvContent.loadUrl(mainUrl);
wbvContent.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
if(progress < 100 && mProgressBar.getVisibility() == ProgressBar.GONE){
mProgressBar.setVisibility(ProgressBar.VISIBLE);
}
mProgressBar.setProgress(progress);
if(progress == 100) {
mProgressBar.setVisibility(ProgressBar.GONE);
}
}
});
}
isFirstTime();
}
public void isFirstTime() {// check if the first time
try{
SharedPreferences mPreference=PreferenceManager.getDefaultSharedPreferences(this);
boolean firstTime=mPreference.getBoolean("firstTime", true);
if(firstTime) {
ShortcutIconResource icon = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
Intent intent = new Intent();
Intent launchIntent = new Intent(this, XaLuanActivity.class);

intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent);
intent.putExtra("android.intent.extra.shortcut.NAME", "Xã Luận");
intent.putExtra("android.intent.extra.shortcut.ICON_RESOURCE", icon); //can also be ignored too
intent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
sendBroadcast(intent);
SharedPreferences.Editor edit=mPreference.edit();
edit.putBoolean("firstTime", false);
edit.commit();
}
}catch (Exception e) {
Log.e(tag+".isFirstTime", e.toString());
}
}


private static class HelloWebViewClient extends WebViewClient {

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

if (url != null && (url.startsWith("market://") || url.indexOf("play.google.com/store/apps/details")!=-1) ) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
//return true;
}
else if(url.endsWith(".apk") || url.indexOf("adsxaluan.apk.vn/download/")!=-1 )
{
view.getContext().startActivity( new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url)) );
}
else {
view.loadUrl(url);
//return false;
}
return true;
}

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, CHIASE_MENU, 0, "Chia Sẻ").setIcon(R.drawable.share);
menu.add(0, TAILAI_MENU, 0, "Tải Lại").setIcon(R.drawable.refresh);
menu.add(0, DANHGIA_MENU, 0, "Đánh Giá").setIcon(R.drawable.rate);
menu.add(0, TROGIUP_MENU, 0, "Trợ Giúp").setIcon(R.drawable.about);
menu.add(0, THOAT_MENU, 0, "Thoát").setIcon(R.drawable.exit);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case CHIASE_MENU: // chia se
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, wbvContent.getTitle());
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, wbvContent.getUrl());
startActivity(Intent.createChooser(sharingIntent,"Chia sẻ"));
break;
case THOAT_MENU: // chia se
System.exit(0);
break;
case TAILAI_MENU: // chia se
wbvContent.reload();
break;
case TROGIUP_MENU: // chia se
Intent intent1=new Intent(this, About.class);
startActivity(intent1);
break;
case DANHGIA_MENU:
String url="market://details?id="+getApplicationContext().getPackageName();
Intent intent2 = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
startActivity(intent2);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if(wbvContent.canGoBack()) {
wbvContent.goBack();
return false;
}else {
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Thoát")
.setMessage("Bạn có muốn thoát khỏi ứng dụng không")
.setPositiveButton("Có", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
XaLuanActivity.this.finish();
}
})
.setNegativeButton("Không", null)
.show();
}
}
return super.onKeyDown(keyCode, event);
}
public static boolean isConnectedNetwork(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
}

最佳答案

对不起,你想做的事是不可能的。出于安全和隐私原因,您的应用程序和 Chrome 的 cookie jar 是完全独立的,彼此无法访问。

关于android - 如何在 webview 和 chrome 中同步 cookie android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21966677/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com