导航:首页 > 操作系统 > androidcachemanager

androidcachemanager

发布时间:2022-10-15 14:16:45

android开发中怎样清除本地缓存文件夹

java">

/** * 本应用数据清除管理器 */

public class DataCleanManager {

/** * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) * * @param context */

public static void cleanInternalCache(Context context) {

deleteFilesByDirectory(context.getCacheDir());

}


/** * 清除本应用所有数据库(/data/data/com.xxx.xxx/databases) * * @param context */

public static void cleanDatabases(Context context) {

deleteFilesByDirectory(new File("/data/data/com.example.orderfood"));

}


/**

* * 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs) * * @param

* context

*/

public static void cleanSharedPreference(Context context) {

deleteFilesByDirectory(new File("/data/data/com.example.orderfood/shared_prefs"));

}


/** * 按名字清除本应用数据库 * * @param context * @param dbName */

public static void cleanDatabaseByName(Context context, String dbName) {

context.deleteDatabase(dbName);

}


/** * 清除/data/data/com.xxx.xxx/files下的内容 * * @param context */

public static void cleanFiles(Context context) {

deleteFilesByDirectory(context.getFilesDir());

}


/**

* * 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param

* context

*/

public static void cleanExternalCache(Context context) {

if (Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED)) {

deleteFilesByDirectory(context.getExternalCacheDir());

}

}


/** * 清除自定义路径下的文件,使用需小心,请不要误删。而且只支持目录下的文件删除 * * @param filePath */

public static void cleanCustomCache(String filePath) {

deleteFilesByDirectory(new File(filePath));

}


/** * 清除本应用所有的数据 * * @param context * @param filepath */

public static void cleanApplicationData(Context context, String... filepath) {

cleanInternalCache(context);

cleanExternalCache(context);

cleanDatabases(context);

cleanSharedPreference(context);

cleanFiles(context);

for (String filePath : filepath) {

cleanCustomCache(filePath);

}

}


/** * 删除方法 这里只会删除某个文件夹下的文件,如果传入的directory是个文件,将不做处理 * * @param directory */

private static void deleteFilesByDirectory(File directory) {

if (directory != null && directory.exists() && directory.isDirectory()) {

for (File item : directory.listFiles()) {

item.delete();

}

}

}

}

⑵ android-cachemanager新版本中用什么方法代替

Android 5.0 是 Google 于 2014 年 10 月 15 日(美国太平洋时间)发布的全新 Android 操作系统。北京时间 2014 年 6 月 26 日 0 时,Google I/O 2014 开发者大会在旧金山正式召开,发布了 Android 5.0 的前身 L(Lollipop)版 Android 开发者预览。今年的三款新 Nexus 设备——Nexus 6、Nexus 9平板及 Nexus Player 将率先搭载 Android 5.0,之前的Nexus4、 Nexus 5、Nexus 7及 Nexus 10将会很快获得更新,而 Google Play 版设备则需要等上几周才能升级。

⑶ android开发文件缓存在什么位置,可以在应用程序中清除。

android开发文件缓存的默认位置一般是在android/data目录下,比如kindle(1st)是在/mnt/sdcard/Android/data目录下,魅族是在/sdcard/Android/data目录下。
将缓存在应用程序中清除:
打开关闭使用缓存,一共有五个种类
//优先使用缓存:
WebView.getSettings().setCacheMod
(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//不使用缓存:
WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
在退出应用的时候加上如下代码
File file = CacheManager.getCacheFileBaseDir();
if (file != null && file.exists() && file.isDirectory()) {
for (File item : file.listFiles()) {
item.delete(); }
file.delete(); }
context.deleteDatabase("WebView.db");
context.deleteDatabase("WebViewCache.db");
以上方法均可实现。

⑷ android4.3下导入osc客户端,CacheManager找不到,求解答

修改成4.0版本,并重新加入import android.webkit.CacheManager; Fix下工程即可
//清除webview缓存
// File file = CacheManager.getCacheFileBaseDir();
// if (file != null && file.exists() && file.isDirectory()) {
// for (File item : file.listFiles()) {
// item.delete();
// }
// file.delete();

// }

⑸ 如何将WebView的缓存设置到缓存至SD卡

楼主可以参考下面这个文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.devahead.androidwebviewcacheonsd"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk android:minSdkVersion="7"/>

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:name="com.devahead.androidwebviewcacheonsd.ApplicationExt">
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:label="Second activity"/>
</application>

</manifest>

Here we have the permissions to write to the SD (the external storage) and access the web. We also have a custom Application class extension called ApplicationExt and a couple of activities, MainActivity and SecondActivity.
The ApplicationExt class is where most of the things are done to have the cache on the SD:
package com.devahead.androidwebviewcacheonsd;

import java.io.File;

import android.app.Application;
import android.os.Environment;

public class ApplicationExt extends Application
{
// NOTE: the content of this path will be deleted
// when the application is uninstalled (Android 2.2 and higher)
protected File extStorageAppBasePath;

protected File extStorageAppCachePath;

@Override
public void onCreate()
{
super.onCreate();

// Check if the external storage is writeable
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
{
// Retrieve the base path for the application in the external storage
File externalStorageDir = Environment.getExternalStorageDirectory();

if (externalStorageDir != null)
{
// {SD_PATH}/Android/data/com.devahead.androidwebviewcacheonsd
extStorageAppBasePath = new File(externalStorageDir.getAbsolutePath() +
File.separator + "Android" + File.separator + "data" +
File.separator + getPackageName());
}

if (extStorageAppBasePath != null)
{
// {SD_PATH}/Android/data/com.devahead.androidwebviewcacheonsd/cache
extStorageAppCachePath = new File(extStorageAppBasePath.getAbsolutePath() +
File.separator + "cache");

boolean isCachePathAvailable = true;

if (!extStorageAppCachePath.exists())
{
// Create the cache path on the external storage
isCachePathAvailable = extStorageAppCachePath.mkdirs();
}

if (!isCachePathAvailable)
{
// Unable to create the cache path
extStorageAppCachePath = null;
}
}
}
}

@Override
public File getCacheDir()
{
// NOTE: this method is used in Android 2.2 and higher

if (extStorageAppCachePath != null)
{
// Use the external storage for the cache
return extStorageAppCachePath;
}
else
{
// /data/data/com.devahead.androidwebviewcacheonsd/cache
return super.getCacheDir();
}
}
}

In the onCreate method we start by checking if the external storage is actually mounted and we can write on it, then we build the base path of the app on the external storage. This path is {SD_PATH}/Android/data/com.devahead.androidwebviewcacheonsd becausecom.devahead.androidwebviewcacheonsd is the package declared in the manifest file and using this path structure makes sure that the entire path and all its content will be automatically deleted by Android 2.2 and higher in case the app is uninstalled avoiding to have garbage files on the SD. Note that this works only in Android 2.2 and higher, while in Android 2.1 the path will not be deleted by the OS so you’ll have to deal with it on your own. The full path for the cache files will be{SD_PATH}/Android/data/com.devahead.androidwebviewcacheonsd/cache, so the base path plus thecache directory. We must also make sure that the path is actually available before using it, so we create all the directories with the mkdirs method in case they don’t already exist.
Now that we have the cache path on the SD, we’re ready to use it and all we have to do is override the getCacheDir method inside our ApplicationExt class. This method is invoked by the cache manager when the application is started so basically we’re saying that the cache will be stored on the SD if it’s available and writeable, while we use the default path in case we’re allowed to use only the internal memory. Android stores all the cache data for our app in the /data/data/com.devahead.androidwebviewcacheonsd/cache directory by default on the internal memory.
We’re done with the implementation for Android 2.2 and higher, but what about Android 2.1? For that version of the OS the cache manager doesn’t use the getCacheDir method of theApplication context, but it uses the one of the Activity context instead. So to make our solution work also with Android 2.1, we must override the getCacheDir method inside our activities.
To immediately understand how it works with Android 2.1, let’s take a look at the activities of the example application. We start with the MainActivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<Button android:id="@+id/startSecondActivityBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Start second activity"/>

<WebView android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

</LinearLayout>

There’s simply a button to start the SecondActivity and a WebView to test our solution. The code for MainActivity looks like this:
package com.devahead.androidwebviewcacheonsd;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener
{
protected WebView webView;
protected Button startSecondActivityBtn;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

webView = ((WebView)findViewById(R.id.webView));
startSecondActivityBtn = ((Button)findViewById(R.id.startSecondActivityBtn));

// Set the listener
startSecondActivityBtn.setOnClickListener(this);

// Initialize the WebView
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);

// Load the URLs inside the WebView, not in the external web browser
webView.setWebViewClient(new WebViewClient());

webView.loadUrl("http://www.google.com");
}

@Override
protected void onDestroy()
{
// Clear the cache (this clears the WebViews cache for the entire application)
webView.clearCache(true);

super.onDestroy();
}

@Override
public File getCacheDir()
{
// NOTE: this method is used in Android 2.1

return getApplicationContext().getCacheDir();
}

@Override
public void onClick(View v)
{
if (v == startSecondActivityBtn)
{
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}
}

As you can see, the getCacheDir method inside the activity simply calls the same method in the Application context, that means that the ApplicationExt method is called. We’ll have to do this for every activity in the app to make sure the cache path is redirected correctly in Android 2.1, but actually in my tests I found that it looks like the cache manager calls thegetCacheDir method inside the first activity that initializes a WebView (if MainActivitydoesn’t use a WebView, but only SecondActivity does, then the getCacheDir method ofSecondActivity will be called by the cache manager and not the one of MainActivity), anyway I think it’s a good idea to make sure the cache directory is consistent across all the activities of the application.
In MainActivity there’s a button that starts SecondActivity. This is just another activity with a WebView that I used to test the calls to the getCacheDir method in Android 2.1 and see what happens if there’s a WebView also in MainActivity or not. This is the layout ofSecondActivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<WebView android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>

</LinearLayout>

And this is its source code:
package com.devahead.androidwebviewcacheonsd;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class SecondActivity extends Activity
{
protected WebView webView;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second);

webView = ((WebView)findViewById(R.id.webView));

// Initialize the WebView
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);
webView.getSettings().setLoadsImagesAutomatically(true);

// Load the URLs inside the WebView, not in the external web browser
webView.setWebViewClient(new WebViewClient());

webView.loadUrl("http://www.google.com");
}

@Override
public File getCacheDir()
{
// NOTE: this method is used in Android 2.1

return getApplicationContext().getCacheDir();
}
}

⑹ android 清除缓存功能如何实现

Android清除本地数据缓存代码:

/* * 文 件 名: DataCleanManager.java * 描 述: 主要功能有清除内/外缓存,清除数据库,清除sharedPreference,清除files和清除自定义目录 */
import java.io.File;

import android.content.Context;
import android.os.Environment;
/** * 本应用数据清除管理器 */

public class DataCleanManager {
/** * 清除本应用内部缓存(/data/data/com.xxx.xxx/cache) * * @param context */
public static void cleanInternalCache(Context context) {
deleteFilesByDirectory(context.getCacheDir());
}
/** * 清除本应用所有数据库(/data/data/com.xxx.xxx/databases) * * @param context */

public static void cleanDatabases(Context context) {
deleteFilesByDirectory(new File("/data/data/"
+ context.getPackageName() + "/databases"));
}
/**

* * 清除本应用SharedPreference(/data/data/com.xxx.xxx/shared_prefs) * * @param
* context
*/
public static void cleanSharedPreference(Context context) {
deleteFilesByDirectory(new File("/data/data/"
+ context.getPackageName() + "/shared_prefs"));
}
/** * 按名字清除本应用数据库 * * @param context * @param dbName */

public static void cleanDatabaseByName(Context context, String dbName) {
context.deleteDatabase(dbName);
}
/** * 清除/data/data/com.xxx.xxx/files下的内容 * * @param context */

public static void cleanFiles(Context context) {
deleteFilesByDirectory(context.getFilesDir());
}
/**

* * 清除外部cache下的内容(/mnt/sdcard/android/data/com.xxx.xxx/cache) * * @param
* context
*/
public static void cleanExternalCache(Context context) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
deleteFilesByDirectory(context.getExternalCacheDir());
}
}
/** * 清除自定义路径下的文件,使用需小心,请不要误删。而且只支持目录下的文件删除 * * @param filePath */

public static void cleanCustomCache(String filePath) {
deleteFilesByDirectory(new File(filePath));
}
/** * 清除本应用所有的数据 * * @param context * @param filepath */

public static void cleanApplicationData(Context context, String... filepath) {
cleanInternalCache(context);
cleanExternalCache(context);
cleanDatabases(context);
cleanSharedPreference(context);
cleanFiles(context);
for (String filePath : filepath) {
cleanCustomCache(filePath);
}
}
/** * 删除方法 这里只会删除某个文件夹下的文件,如果传入的directory是个文件,将不做处理 * * @param directory */

private static void deleteFilesByDirectory(File directory) {
if (directory != null && directory.exists() && directory.isDirectory()) {
for (File item : directory.listFiles()) {
item.delete();
}
}
}
}

主要功能清除内/外缓存,清除数据库,清除sharedPreference,清除files和清除自定义目录

⑺ 如何清理android sdk manager的download cache

  1. downloadcache是手机里的高速缓冲文件,可以快速打开阅读中的小说等功能,该文件是由系统自动生成。

  2. 可以在手机中找到该文件夹直接删除。

  3. 因为是系统自动生成,所以即使删除该文件,下次打开文件时依然会自动生成。

⑻ 简述android平台提供了哪些数据存储方法

数据存储在开发中是使用最频繁的,Android平台主要有5种实现数据存储的方式。
第1种: 使用SharedPreferences存储数据
SharedPreferences是Android平台上一个轻量级的存储类,主要是保存一些常用的配置比如窗口状态,一般在Activity中 重载窗口状态onSaveInstanceState保存一般使用SharedPreferences完成,它提供了Android平台常规的Long长 整形、Int整形、String字符串型的保存。
它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息。
其存储位置在/data/data/<包名>/shared_prefs目录下。
SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。
实现SharedPreferences存储的步骤如下:
一、根据Context获取SharedPreferences对象
二、利用edit()方法获取Editor对象。
三、通过Editor对象存储key-value键值对数据。
四、通过commit()方法提交数据。
SharedPreferences对象与SQLite数据库相比,免去了创建数据库,创建表,写SQL语句等诸多操作,相对而言更加方便,简洁。但是SharedPreferences也有其自身缺陷,比如其职能存储boolean,int,float,long和String五种简单的数据类型,比如其无法进行条件查询等。所以不论SharedPreferences的数据存储操作是如何简单,它也只能是存储方式的一种补充,而无法完全替代如SQLite数据库这样的其他数据存储方式。
第2种: 内部文件存储数据
关于文件存储,Activity提供了openFileOutput()方法可以用于把数据输出到文件中,具体的实现过程与在J2SE环境中保存数据到文件中是一样的。
文件可用来存放大量数据,如文本、图片、音频等。
默认位置:/data/data/<包>/files/***.***。
openFileOutput()方法的第一参数用于指定文件名称,不能包含路径分隔符“/” ,如果文件不存在,Android 会自动创建它。
创建的文件保存在/data/data//files目录,如: /data/data/cn.itcast.action/files/itcast.txt ,通过点击Eclipse菜单“Window”-“Show View”-“Other”,在对话窗口中展开android文件夹,选择下面的File Explorer视图,然后在File Explorer视图中展开/data/data//files目录就可以看到该文件。
openFileOutput()方法的第二参数用于指定操作模式,有四种模式,分别为:
Context.MODE_PRIVATE = 0
Context.MODE_APPEND = 32768
Context.MODE_WORLD_READABLE = 1
Context.MODE_WORLD_WRITEABLE = 2
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
如果希望文件被其他应用读和写,可以传入: openFileOutput(“itcast.txt”, Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE); android有一套自己的安全模型,当应用程序(.apk)在安装时系统就会分配给他一个userid,当该应用要去访问其他资源比如文件的时候,就需要userid匹配。默认情况下,任何应用创建的文件,sharedpreferences,数据库都应该是私有的(位于/data/data//files),其他程序无法访问。
除非在创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE ,只有这样其他程序才能正确访问。 对于私有文件只能被创建该文件的应用访问,如果希望文件能被其他应用读和写,可以在创建文件时,指定Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE权限。
Activity还提供了getCacheDir()和getFilesDir()方法: getCacheDir()方法用于获取/data/data//cache目录 getFilesDir()方法用于获取/data/data//files目录。
第3种: 外部文件存储数据
把文件存入SDCard:
使用Activity的openFileOutput()方法保存文件,文件是存放在手机空间上,一般手机的存储空间不是很大,存放些小文件还行,如果要存放像视频这样的大文件,是不可行的。对于像视频这样的大文件,我们可以把它存放在SDCard。
SDCard是干什么的?你可以把它看作是移动硬盘或U盘。 在模拟器中使用SDCard,你需要先创建一张SDCard卡(当然不是真的SDCard,只是镜像文件)。
创建SDCard可以在Eclipse创建模拟器时随同创建,也可以使用DOS命令进行创建,如下: 在Dos窗口中进入android SDK安装路径的tools目录,输入以下命令创建一张容量为2G的SDCard,文件后缀可以随便取,建议使用.img: mksdcard 2048M D:\AndroidTool\sdcard.img 在程序中访问SDCard,你需要申请访问SDCard的权限。
在AndroidManifest.xml中加入访问SDCard的权限如下:
要往SDCard存放文件,程序必须先判断手机是否装有SDCard,并且可以进行读写。
注意:访问SDCard必须在AndroidManifest.xml中加入访问SDCard的权限。
Environment.getExternalStorageState()方法用于获取SDCard的状态,如果手机装有SDCard,并且可以进行读写,那么方法返回的状态等于Environment.MEDIA_MOUNTED。
Environment.getExternalStorageDirectory()方法用于获取SDCard的目录,当然要获取SDCard的目录,你也可以这样写:
第4种: SQLite数据库存储数据
SQLite是轻量级嵌入式数据库引擎,它支持 SQL 语言,并且只利用很少的内存就有很好的性能。此外它还是开源的,任何人都可以使用它。许多开源项目((Mozilla, PHP, Python)都使用了 SQLite.SQLite 由以下几个组件组成:SQL 编译器、内核、后端以及附件。SQLite 通过利用虚拟机和虚拟数据库引擎(VDBE),使调试、修改和扩展 SQLite 的内核变得更加方便。
特点:
面向资源有限的设备,
没有服务器进程,
所有数据存放在同一文件中跨平台,
可自由复制。
SQLite 基本上符合 SQL-92 标准,和其他的主要 SQL 数据库没什么区别。它的优点就是高效,Android 运行时环境包含了完整的 SQLite。
SQLite 和其他数据库最大的不同就是对数据类型的支持,创建一个表时,可以在 CREATE TABLE 语句中指定某列的数据类型,但是你可以把任何数据类型放入任何列中。当某个值插入数据库时,SQLite 将检查它的类型。如果该类型与关联的列不匹配,则 SQLite 会尝试将该值转换成该列的类型。如果不能转换,则该值将作为其本身具有的类型存储。比如可以把一个字符串(String)放入 INTEGER 列。SQLite 称这为“弱类型”(manifest typing.)。 此外,SQLite 不支持一些标准的 SQL 功能,特别是外键约束(FOREIGN KEY constrains),嵌套 transcaction 和 RIGHT OUTER JOIN 和 FULL OUTER JOIN, 还有一些 ALTER TABLE 功能。 除了上述功能外,SQLite 是一个完整的 SQL 系统,拥有完整的触发器,交易等等。
Android 集成了 SQLite 数据库 Android 在运行时(run-time)集成了 SQLite,所以每个 Android 应用程序都可以使用 SQLite 数据库。
对于熟悉 SQL 的开发人员来时,在 Android 开发中使用 SQLite 相当简单。但是,由于 JDBC 会消耗太多的系统资源,所以 JDBC 对于手机这种内存受限设备来说并不合适。因此,Android 提供了一些新的 API 来使用 SQLite 数据库,Android 开发中,程序员需要学使用这些 API。
数据库存储在 data/< 项目文件夹 >/databases/ 下。 Android 开发中使用 SQLite 数据库 Activites 可以通过 Content Provider 或者 Service 访问一个数据库。
创建数据库 Android 不自动提供数据库。在 Android 应用程序中使用 SQLite,必须自己创建数据库,然后创建表、索引,填充数据。
Android 提供了 SQLiteOpenHelper 帮助你创建一个数据库,你只要继承 SQLiteOpenHelper 类,就可以轻松的创建数据库。SQLiteOpenHelper 类根据开发应用程序的需要,封装了创建和更新数据库使用的逻辑。
SQLiteOpenHelper 的子类,至少需要实现三个方法:
1 构造函数,调用父类 SQLiteOpenHelper 的构造函数。这个方法需要四个参数:上下文环境(例如,一个 Activity),数据库名字,一个可选的游标工厂(通常是 Null),一个代表你正在使用的数据库模型版本的整数。
2 onCreate()方法,它需要一个 SQLiteDatabase 对象作为参数,根据需要对这个对象填充表和初始化数据。
3 onUpgrage() 方法,它需要三个参数,一个 SQLiteDatabase 对象,一个旧的版本号和一个新的版本号,这样你就可以清楚如何把一个数据库从旧的模型转变到新的模型。
接下来讨论具体如何创建表、插入数据、删除表等等。调用 getReadableDatabase() 或 getWriteableDatabase() 方法,你可以得到 SQLiteDatabase 实例,具体调用那个方法,取决于你是否需要改变数据库的内容:
update()方法有四个参数,分别是表名,表示列名和值的 ContentValues 对象,可选的 WHERE 条件和可选的填充 WHERE 语句的字符串,这些字符串会替换 WHERE 条件中的“?”标记。
update() 根据条件,更新指定列的值,所以用 execSQL() 方法可以达到同样的目的。 WHERE 条件和其参数和用过的其他 SQL APIs 类似。
delete() 方法的使用和 update() 类似,使用表名,可选的 WHERE 条件和相应的填充 WHERE 条件的字符串。 查询数据库 类似 INSERT, UPDATE, DELETE,有两种方法使用 SELECT 从 SQLite 数据库检索数据。
1 .使用 rawQuery() 直接调用 SELECT 语句; 使用 query() 方法构建一个查询。
Raw Queries 正如 API 名字,rawQuery() 是最简单的解决方法。通过这个方法你就可以调用 SQL SELECT 语句。
例如: Cursor c=db.rawQuery( “SELECT name FROM sqlite_master WHERE type=’table’ AND name=’mytable’”, null);
在上面例子中,我们查询 SQLite 系统表(sqlite_master)检查 table 表是否存在。返回值是一个 cursor 对象,这个对象的方法可以迭代查询结果。 如果查询是动态的,使用这个方法就会非常复杂。
例如,当你需要查询的列在程序编译的时候不能确定,这时候使用 query() 方法会方便很多。
Regular Queries query() 方法用 SELECT 语句段构建查询。SELECT 语句内容作为 query() 方法的参数,比如:要查询的表名,要获取的字段名,WHERE 条件,包含可选的位置参数,去替代 WHERE 条件中位置参数的值,GROUP BY 条件,HAVING 条件。 除了表名,其他参数可以是 null。所以,以前的代码段可以可写成:
String[] columns={“ID”, ”inventory”};
Java代码
String[] parms={"snicklefritz"}; Cursor result=db.query("widgets", columns, "name=?",parms, null, null, null);
使用游标
不管你如何执行查询,都会返回一个 Cursor,这是 Android 的 SQLite 数据库游标,
使用游标,你可以:
通过使用 getCount() 方法得到结果集中有多少记录;
通过 moveToFirst(), moveToNext(), 和 isAfterLast() 方法遍历所有记录;
通过 getColumnNames() 得到字段名;
通过 getColumnIndex() 转换成字段号;
通过 getString(),getInt() 等方法得到给定字段当前记录的值;
通过 requery() 方法重新执行查询得到游标;
通过 close() 方法释放游标资源;
在 Android 中使用 SQLite 数据库管理工具 在其他数据库上作开发,一般都使用工具来检查和处理数据库的内容,而不是仅仅使用数据库的 API。
使用 Android 模拟器,有两种可供选择的方法来管理数据库。
首先,模拟器绑定了 sqlite3 控制台程序,可以使用 adb shell 命令来调用他。只要你进入了模拟器的 shell,在数据库的路径执行 sqlite3 命令就可以了。
数据库文件一般存放在: /data/data/your.app.package/databases/your-db-name 如果你喜欢使用更友好的工具,你可以把数据库拷贝到你的开发机上,使用 SQLite-aware 客户端来操作它。这样的话,你在一个数据库的拷贝上操作,如果你想要你的修改能反映到设备上,你需要把数据库备份回去。
把数据库从设备上考出来,你可以使用 adb pull 命令(或者在 IDE 上做相应操作)。
存储一个修改过的数据库到设备上,使用 adb push 命令。 一个最方便的 SQLite 客户端是 FireFox SQLite Manager 扩展,它可以跨所有平台使用。
如果你想要开发 Android 应用程序,一定需要在 Android 上存储数据,使用 SQLite 数据库是一种非常好的选择。
第五种: 网络存储数据
前面介绍的几种存储都是将数据存储在本地设备上,除此之外,还有一种存储(获取)数据的方式,通过网络来实现数据的存储和获取。

我们可以调用WebService返回的数据或是解析HTTP协议实现网络数据交互。

⑼ android现在怎么清除webview缓存不能用cachemanager.getcachefilebasedir

一、清除cookie

public static void clearCookies(Context context) {
// Edge case: an illegal state exception is thrown if an instance of
// CookieSyncManager has not be created. CookieSyncManager is normally
// created by a WebKit view, but this might happen if you start the
// app, restore saved state, and click logout before running a UI
// dialog in a WebView -- in which case the app crashes
@SuppressWarnings("unused")
CookieSyncManager cookieSyncMngr =
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.removeAllCookie();
}

这是facebook sdk的源码,我不知道第一句到底起了什么作用?

二、清除webview缓存,查看root过的手机data下的文件,会发现有这个东西:webview命名的东西

删除保存于手机上的缓存.

// clear the cache before time numDays
private int clearCacheFolder(File dir, long numDays) {
int deletedFiles = 0;
if (dir!= null && dir.isDirectory()) {
try {
for (File child:dir.listFiles()) {
if (child.isDirectory()) {
deletedFiles += clearCacheFolder(child, numDays);
}
if (child.lastModified() < numDays) {
if (child.delete()) {
deletedFiles++;
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
return deletedFiles;
}

打开关闭使用缓存
//优先使用缓存:
WebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

//不使用缓存:
WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);

在退出应用的时候加上如下代码
File file = CacheManager.getCacheFileBaseDir();
if (file != null && file.exists() && file.isDirectory()) {
for (File item : file.listFiles()) {
item.delete();
}
file.delete();
}

context.deleteDatabase("webview.db");
context.deleteDatabase("webviewCache.db");

发现这个问题,一个朋友在iteye上问的:

Android的CookieManager只提供了removeAllCookies方法,用来删除所有的cookie,有什么办法只删除和特定url关联的cookie呢?本来打算使用setCookie(url, value)将指定url关联的cookie设为空串,但试了一下发现这个方法只是在已有的基础上继续添加cookie,并不能重置已有的cookie。

有朋友给打答案:

/**
* 同步一下cookie
*/
public static void synCookies(Context context, String url) {
CookieSyncManager.createInstance(context);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieManager.removeSessionCookie();//移除
cookieManager.setCookie(url, cookies);//指定要修改的cookies
CookieSyncManager.getInstance().sync();
}

⑽ android 应用程序开发中,清除缓存的功能怎么做

android开发文件缓存的默认位置一般是在android/data目录下,比如kindle(1st)是在/mnt/sdcard/Android/data目录下,魅族是在/sdcard/Android/data目录下。
将缓存在应用程序中清除:
打开关闭使用缓存,一共有五个种类
//优先使用缓存:
WebView.getSettings().setCacheMod
(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//不使用缓存:
WebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
在退出应用的时候加上如下代码
File file = CacheManager.getCacheFileBaseDir();
if (file != null && file.exists() && file.isDirectory()) {
for (File item : file.listFiles()) {
item.delete(); }
file.delete(); }
context.deleteDatabase("WebView.db");
context.deleteDatabase("WebViewCache.db");
以上方法均可实现。

阅读全文

与androidcachemanager相关的资料

热点内容
传奇引擎修改在线时间命令 浏览:105
php取域名中间 浏览:896
cad命令栏太小 浏览:830
php开发环境搭建eclipse 浏览:480
qt文件夹名称大全 浏览:212
金山云服务器架构 浏览:230
安卓系统笔记本怎么切换系统 浏览:618
u盘加密快2个小时还没有搞完 浏览:93
小米有品商家版app叫什么 浏览:94
行命令调用 浏览:435
菜鸟裹裹员用什么app 浏览:273
穷查理宝典pdf下载 浏览:514
csgo您已被禁用此服务器怎么办 浏览:398
打开加密软件的方法 浏览:156
云存储服务器可靠吗 浏览:967
2核1g的云服务器能带动游戏嘛 浏览:898
逆命20解压码 浏览:146
徐州办犬证需要下载什么app 浏览:1002
百保盾是什么样的app 浏览:699
文件和文件夹的命名规格 浏览:798