应用间共享数据方法(一)---sharepreferce

时间:2023-03-09 09:56:35
应用间共享数据方法(一)---sharepreferce

SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。

SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下:

一、首先分析几个方法:

1、getSharedPreferences(name,mode)

方法的第一个参数用于指定该文件的名称,名称不用带后缀,后缀会由Android自动加上;

方法的第二个参数指定文件的操作模式,共有六种操作模式。

六种操作模式分别为:

1). MODE_APPEND: 追加方式存储

2). MODE_PRIVATE: 私有方式存储,其他应用无法访问

3). MODE_ENABLE_WRITE_AHEAD_LOGGING   数据库打开默认启用了写前日志记录

4). MODE_MULTI_PROCESS:多个进程间共享,但在2.3之后不再使用了

5). MODE_WORLD_READABLE: 表示当前文件可以被其他应用读取(不推荐使用)

6). MODE_WORLD_WRITEABLE: 表示当前文件可以被其他应用写入(不推荐使用)

SharedPreferences share = getSharedPreferences("lsf", MODE_PRIVATE);   

2、edit()方法获取editor对象

editor存储对象采用key-value键值对进行存放

通过commit()方法提交数据

 SharedPreferences share = getSharedPreferences("jackie", MODE_WORLD_READABLE);   //其他应用可以获得
SharedPreferences.Editor edit = share.edit();
edit.putString(“password”, ""+987654321);
edit.putBoolean("pass_state", true);
edit.commit();

与之对应其他应用获取数据的方法:

otherAppsContext = createPackageContext("com.jackie.contextprovider", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("jackie", Context.MODE_WORLD_READABLE);
String password= sharedPreferences.getString("password", "");
boolean pass_state= sharedPreferences.getBoolean("pass_state", false); //第二个参数为缺省值,如果preference中不存在该key,将返回缺省值
mTextView.setText("password:"+password+"||||||||"+"pass_state:"+pass_state);

如果你想要删除通过SharedPreferences产生的文件,可以通过以下方法:

File file= new File("/data/data/"+getPackageName().toString()+"/shared_prefs","Activity.xml");
if(file.exists()){
file.delete();
Toast.makeText(TestActivity.this, "删除成功", Toast.LENGTH_LONG).show();
}

二、应用实例:

一个应用中设置一个按钮,可按下触发设置为共享或不共享其SharedPreferences产生的文件,

另一个应用通过按钮按下触发获得之前应用中的数据。

1、共享数据文件的应用:

package com.jackie.contextprovider;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button; public class MainActivity extends Activity { public static final String NEW_LIFEFROM_DETECTED = "com.jackie.contextprovider";
public static final String TAG = "testprovider"; private Button snedButton;
private boolean isShare = true; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); snedButton = (Button) findViewById(R.id.send_message);
} public void sendMessage(View v){
if(isShare){
SharedPreferences share = getSharedPreferences("jackie", MODE_WORLD_READABLE);
SharedPreferences.Editor edit = share.edit();
edit.putString(“password”, ""+987654321);
edit.putBoolean("pass_state", true);
edit.commit();
isShare = false;
snedButton.setText(R.string.no_send);
}else{
SharedPreferences share = getSharedPreferences("jackie", MODE_PRIVATE);
SharedPreferences.Editor edit = share.edit();
edit.clear();
edit.commit();
isShare = true;
snedButton.setText(R.string.send);
}
}
}

布局配置文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" > <Button
android:id="@+id/send_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="@string/send"
/> </RelativeLayout>

2、读取共享数据的应用

package com.jackie.contextuser;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView; public class MainActivity extends Activity { Button mButton;
TextView mTextView;
Context otherAppsContext; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.get_message);
mTextView = (TextView) findViewById(R.id.write_message);
otherAppsContext = null;
} @Override
protected void onResume(){
super.onResume();
otherAppsContext = null;
} @Override
protected void onStop(){
super.onStop();
otherAppsContext = null;
} public void getMessage(View v) throws NameNotFoundException{
otherAppsContext = createPackageContext("com.tcl.contextprovider", Context.CONTEXT_IGNORE_SECURITY);
SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("jackie", Context.MODE_WORLD_READABLE);
String password = sharedPreferences.getString("password", "");
boolean pass_state= sharedPreferences.getBoolean("pass_state", false);
mTextView.setText("password :"+password +"||||||||"+"pass_state:"+pass_state);
}
}

布局配置文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" > <Button
android:id="@+id/get_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="getMessage"
android:text="@string/get"/> <TextView
android:id="@+id/write_message"
android:layout_below="@id/get_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </RelativeLayout>
--------------------------------------------------------------------------------------------
刚开始学习,写博客只是希望能记录自己的成长轨迹,欢迎大家指点。

相关文章