2016-11-15 85 views
1

我試圖實現完全傾斜(https://github.com/adtile/Full-Tilt)羅盤例子(https://richtr.github.io/Marine-Compass)後保留最後指南針位置。當您重新啓動應用程序,關閉它(通過刷卡,或者多任務,...)羅盤北位置不被複位和最後的位置保持後的Android的WebView的WebView中應用程序重新啓動,而不是重新

我的問題是。

如何重現:

  1. 開始應用:例如北在頂部(海洋羅盤不 點向北鏈接這只是一個例子。)
  2. 旋轉手機來點在其他位置,例如南。
  3. 關閉應用(通過多任務,輕掃或任務管理器,...)
  4. 再次旋轉您的手機上的其他位置點,例如西
  5. 重新啓動應用程序:指南針指向最後一個位置,南方爲這個 的例子。而不是重新設置並指向真實的位置。西在 我們的例子。

我嘗試了很多在論壇上找到解決方案,但沒有任何工程。像

  • ClearView的()
  • clearHistory()
  • 破壞()
  • clearCache()
  • clearFormData()
  • 清除Cookies
  • setCacheMode(WebSettings.LOAD_NO_CACHE)
  • setDomStorageEnabled(false)
  • setAppCacheEnabled(假)
  • 使用loadURL( 「關於:空白」)

有任何人的想法來解決這個問題?

找到下面的代碼示例...

MainActivity.java:

public class MainActivity extends AppCompatActivity { 

//fields 
private final String mViewUrl = "https://richtr.github.io/Marine-Compass"; 

private WebView mWebView; 


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

    mWebView = ((WebView) findViewById(R.id.testView)); 
    mWebView.loadUrl("about:blank"); 
    String userAgent = "Mozilla/5.0 (Linux; Android 6.0; Android SDK built for x86 Build/MASTER; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/44.0.2403.119 Mobile Safari/537.36"; 
    mWebView.getSettings().setUserAgentString(userAgent); 
    mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 
    mWebView.getSettings().setAppCacheEnabled(false); 
    mWebView.getSettings().setDomStorageEnabled(false); 
    mWebView.clearFormData(); 
    mWebView.clearHistory(); 
    mWebView.clearCache(true); 
    this.clearCookies(this); 
    mWebView.getSettings().setJavaScriptEnabled(true); 
    mWebView.setWebViewClient(new WebViewClient()); 
    mWebView.loadUrl(mViewUrl); 

} 

@SuppressWarnings("deprecation") 
public static void clearCookies(Context context) 
{ 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { 
     CookieManager.getInstance().removeAllCookies(null); 
     CookieManager.getInstance().flush(); 
    } else 
    { 
     CookieSyncManager cookieSyncMngr=CookieSyncManager.createInstance(context); 
     cookieSyncMngr.startSync(); 
     CookieManager cookieManager=CookieManager.getInstance(); 
     cookieManager.removeAllCookie(); 
     cookieManager.removeSessionCookie(); 
     cookieSyncMngr.stopSync(); 
     cookieSyncMngr.sync(); 
    } 
} 
} 

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.kedros.test"> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.kedros.test.MainActivity"> 

<WebView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/testView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
/> 

+0

需要清除的數據編程在這裏看到:http://stackoverflow.com/questions/6134103/clear-應用程序,數據編程方式 –

+0

謝謝,但在哪裏調用clearApplicationData方法?如果用戶關閉多任務應用程序,則無法捕獲事件。應用程序剛剛停止沒有任何事件 –

回答

0

調用它的onStop)方法,並把你的onCreate代碼的onResume()方法

+0

嗨Najeeb,我嘗試調用方法來清除onStop()方法中的數據(我使用此方法從此鏈接https://www.hrupin.com/2011/11/how-to-clear-user-data-in -your-android-application-programmatically),並將創建代碼放在onResume()中,但不起作用。沒有變化。 –

相關問題