android 两个apk之间恭喜数据SharedPreferences
2012-03-27 18:02
441 查看
1主apk
package com.android.sharefres.main;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SharefresMainActivity extends Activity {
/** Called when the activity is first created. */
private String TAG="SharefresMainActivity";
private TextView mTextView;
private SharedPreferences mSharedPreferences;
private Context mContext;
private void init(){
try {
mContext=createPackageContext("com.android.sharefres.second",Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
mSharedPreferences=mContext.getSharedPreferences("device",MODE_WORLD_READABLE+MODE_WORLD_WRITEABLE);
Log.i(TAG, mSharedPreferences.getInt("c", 0)+"=====mSharedPreferences==");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.sharefres.main"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="com.android.sharefres"
>
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SharefresMainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
次apk
package com.android.sharefres.second;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SharefresSecondActivity extends Activity {
/** Called when the activity is first created. */
private SharedPreferences mSharedPreferences;
private Context mContext;
private void init(){
mContext=getApplicationContext();
mSharedPreferences=mContext.getSharedPreferences("device",Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE);
Editor edit=mSharedPreferences.edit();
edit.putInt("c", 10);
edit.commit();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.sharefres.second"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="com.android.sharefres"
>
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SharefresSecondActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
注意两个apk都要有同一进程id即是:android:sharedUserId="com.android.sharefres"
同时注意,写文件的权限要为可读可写,否则才可以访问。
package com.android.sharefres.main;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SharefresMainActivity extends Activity {
/** Called when the activity is first created. */
private String TAG="SharefresMainActivity";
private TextView mTextView;
private SharedPreferences mSharedPreferences;
private Context mContext;
private void init(){
try {
mContext=createPackageContext("com.android.sharefres.second",Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
mSharedPreferences=mContext.getSharedPreferences("device",MODE_WORLD_READABLE+MODE_WORLD_WRITEABLE);
Log.i(TAG, mSharedPreferences.getInt("c", 0)+"=====mSharedPreferences==");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.sharefres.main"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="com.android.sharefres"
>
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SharefresMainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
次apk
package com.android.sharefres.second;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class SharefresSecondActivity extends Activity {
/** Called when the activity is first created. */
private SharedPreferences mSharedPreferences;
private Context mContext;
private void init(){
mContext=getApplicationContext();
mSharedPreferences=mContext.getSharedPreferences("device",Context.MODE_WORLD_READABLE|Context.MODE_WORLD_WRITEABLE);
Editor edit=mSharedPreferences.edit();
edit.putInt("c", 10);
edit.commit();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.sharefres.second"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="com.android.sharefres"
>
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".SharefresSecondActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
注意两个apk都要有同一进程id即是:android:sharedUserId="com.android.sharefres"
同时注意,写文件的权限要为可读可写,否则才可以访问。
相关文章推荐
- 【Android游戏开发之五】游戏注册界面Demo-实现两个Activity之间的切换与数据交互!
- Android两个Activity之间的数据交换
- android之bundle传递数据--两个activities之间
- Android中两个Activity之间数据传递及返回
- Android 两个Activity之间传递数据及返回
- Android笔记——不同apk之间传递参数与数据
- Android之两个activity之间传递数据且有返回值
- Android中两个Activity之间数据传递及返回
- 浅谈android中如何在两个Activity之间互传数据
- 两个apk之间数据通信。(AIDL通信)
- 【Android游戏开发之五】游戏注册界面Demo-实现两个Activity之间的切换与数据交互!
- Android中两个Activity之间数据传递及返回
- Android中在两个Activity之间进行数据传递
- android学习:两个Activity之间的数据传输
- android之bundle传递数据--两个activities之间
- Android 在两个 Activity 之间传递数据
- Android成长之路-关于bundle实现两个activity之间的数据传递
- 两个apk之间对应传输数据
- 【Android游戏开发之五】游戏注册界面Demo-实现两个Activity之间的切换与数据交互!
- Android消息机制之实现两个不同线程之间相互传递数据相互调用