【发布时间】:2014-01-31 15:51:56
【问题描述】:
我正在使用 jquery mobile 开发一个纯 HTML 5 移动应用程序,除了加载我的主 HTML 文件之外,对 Android 代码的依赖非常少。
以下是我的 MainActivity 代码。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main);
WebView webView = new WebView(this);
WebSettings settings = webView.getSettings();
// TO enable JS
settings.setJavaScriptEnabled(true);
// To enable Localstorage
settings.setDomStorageEnabled(true);
webView.loadUrl("file:///android_asset/main.html");
setContentView(webView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.main, menu);
return true;
}
以下是我的 Mainifest 文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android_playlist"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.android_playlist.MainActivity"
android:label="@string/app_name"
android:configChanges="orientation|keyboard|keyboardHidden"
android:screenOrientation="sensor" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
根据 android 文档,当方向改变时,Android 会重新加载 Activity。 就我而言,我的单个活动是一个包含视频列表的 HTML5 文件。现在,当我在我的 Android 手机上对此进行测试时,页面会在我更改方向时自行重新加载。 我想避免这种情况。有人可以建议我解决这个问题。
注意:在我的活动中,我只是加载此页面,其余所有代码都在客户端,即使用 jquery mobile,所以我不确定如何告诉我的 Android 保存我的 HTML5 页面状态。
此外,有时我注意到,当应用程序崩溃或我多次点击返回按钮以退出 应用程序时,我的 localStorage 会变得清晰。
我正在使用以下语法将任何项目存储到 localStorage。
localStorage.setItem() and localStorage.getItem()
当我将 json obj 存储到 localStorage 中时,我还使用了以下 2 个实用程序函数。
Storage.prototype.setObj = function(key, obj) {
return this.setItem(key, JSON.stringify(obj));
}
Storage.prototype.getObj = function(key) {
if (this.getItem(key) != "undefined") {
return JSON.parse(this.getItem(key));
} else {
return null;
}
}
【问题讨论】:
标签: android html jquery-mobile local-storage