您的位置:首页 > 移动开发 > Android开发

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"

同时注意,写文件的权限要为可读可写,否则才可以访问。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息