package com.pluralsight.example.ShareApp1;
import android.app.Activity;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.TextView;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class ShareApp1Activity extends Activity {
private static final String LOG_TAG = "ShareApp1Activity";
private static final String SHARE_APP2_PKG_NAME = "com.pluralsight.example.ShareApp2";
private static final String DATA_DIR_FILES_DIR = "/files";
private static final String SHARE_APP2_DATA_FILE = "data.txt";
private PackageManager mPM;
private TextView mApp2Path;
private TextView mApp2Data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Get the package manager, we will use it to find the app info
// for our other package.
mPM = getPackageManager();
// Get the UI components we need to set as part of our activity.
// This is going to include a text field showing where the other
// app is installed as well as a graphic component found in the
// other package.
mApp2Path = (TextView)findViewById(R.id.share2_install_path);
mApp2Data = (TextView)findViewById(R.id.data_file);
}
@Override
public void onStart() {
String path = null;
String data = null;
super.onStart();
// Use the PackageManager to retrieve the details for our other
// package.
try {
ApplicationInfo ai =
mPM.getApplicationInfo(SHARE_APP2_PKG_NAME,
PackageManager.GET_META_DATA);
path = ai.dataDir;
path += DATA_DIR_FILES_DIR;
File inFile = new File(path, SHARE_APP2_DATA_FILE);
FileInputStream in = new FileInputStream(inFile);
int len = (int)inFile.length();
if (len == 0) {
in.close();
throw new IOException();
}
byte[] dataBytes = new byte[len];
in.read(dataBytes, 0, len);
in.close();
data = new String(dataBytes);
} catch (PackageManager.NameNotFoundException e) {
path = getString(R.string.app2_not_found);
} catch (FileNotFoundException e2) {
data = getString(R.string.no_data_file);
} catch (IOException e3) {
data = getString(R.string.no_data);
}
mApp2Path.setText(path);
mApp2Data.setText(data);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pluralsight.example.ShareApp1"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="pluralsight.example.shareId">
<uses-sdk android:minSdkVersion="14"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="ShareApp1Activity"
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>
package com.pluralsight.example.ShareApp2;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class ShareApp2Activity extends Activity {
private static final String DATA_FILE_NAME = "data.txt";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
public void onStart() {
FileOutputStream out = null;
super.onStart();
// Write out a simple data field.
String dataStr = getString(R.string.data);
try {
out = openFileOutput(DATA_FILE_NAME, 0);
out.write(dataStr.getBytes(), 0, dataStr.length());
out.close();
} catch (FileNotFoundException e) {
Toast.makeText(this, getString(R.string.nofile), Toast.LENGTH_LONG).show();
} catch (IOException e) {
Toast.makeText(this, getString(R.string.ioerr), Toast.LENGTH_LONG).show();
finish();
if (out != null) {
try {
out.close();
} catch (IOException e2) {
// ignore
}
}
return;
}
Toast.makeText(this, getString(R.string.now_load_1), Toast.LENGTH_LONG).show();
finish();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pluralsight.example.ShareApp2"
android:versionCode="1"
android:versionName="1.0"
android:sharedUserId="pluralsight.example.shareId">
<uses-sdk android:minSdkVersion="14"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="ShareApp2Activity"
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>