我成功登录网页后,我的应用程序需要返回到我的主屏幕,但我的代码不起作用。有谁知道这是什么问题?我一直在想如何整天这样做,但我无法弄清楚。在此先感谢!!!
package com.example.loginapplication;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import android.graphics.Bitmap;
public class LogInPage extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Allow the title bar to show loading progress.
requestWindowFeature(Window.FEATURE_PROGRESS);
WebView webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://myLogInPage.com");
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress){
// Activities and WebViews measure progress with different scales.
// The progress meter will automatically disappear when we reach 100%
setProgress(progress * 100);
}
});
webview.setWebViewClient(new WebViewClient(){
//When start to load page, show URL in activity's title bar
@Override
public void onPageStarted(WebView view, String url,Bitmap favicon) {
setTitle(url);
}
@Override
public void onPageFinished(WebView view, String url) {
CookieSyncManager.getInstance().sync();
// Get the cookie from cookie jar.
String cookie = CookieManager.getInstance().getCookie(url);
if (cookie == null) {
return;
}
String[] pairs = cookie.split(";");
for (int i = 0; i < pairs.length; ++i) {
String[] parts = pairs[i].split("=", 2);
// If token is found, return it to the calling activity.
if (parts.length == 2 && parts[0].equalsIgnoreCase("oauth_token")) {
Intent result = new Intent();
result.putExtra("token", parts[1]);
setResult(RESULT_OK, result);
String token = result.getStringExtra("token");
if (token != null) {
//return back to my home screen
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
finish();
}
}
}
});
}
}
答案 0 :(得分:0)
在else语句中完成工作正常。
if (token != null) {
//return back to my home screen
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}else{
finish();
}