3

I have two application , com.appone.one and com.apptwo.two.

I want transfer data from appone to apptwo ,I want when a data transferred to apptwo , apptwo opens or if open only come up(onResume) and show that data. I wrote this code:

com.appone.one:

 Intent i = new Intent(Intent.ACTION_DATE_CHANGED);
                    PackageManager manager = getPackageManager();
                    i = manager.getLaunchIntentForPackage("com.apptwo.two");
                    i.putExtra("MessageText",""+Connect.MessageArrive.toString());
                   i.addCategory(Intent.CATEGORY_LAUNCHER); 
startActivity(i);

com.apptwo.two:

@Override
    public void onResume() {
        super.onResume();
        String name=getIntent().getStringExtra("MessageText");
        Toast.makeText(getApplicationContext(),String.valueOf(name), Toast.LENGTH_LONG).show();
    }  

I want only write this line :

String name=getIntent().getStringExtra("MessageText");

in OnResume because I don't want apptwpo load again,If I write this line in onCreate , my code works fine. but I want that in onResume. Now apptwo returns null :(

what should I do? thanks in advance

Ghasem
  • 14,455
  • 21
  • 138
  • 171
  • Possible duplicate of [How to send data from one application to other application in android?](http://stackoverflow.com/questions/14355860/how-to-send-data-from-one-application-to-other-application-in-android) –  Apr 11 '16 at 04:33
  • no , I test that post now, didn't work – Bahar Azartoos Apr 11 '16 at 04:41

7 Answers7

3

You can use Content Providers for data sharing between your apps. You can learn more about Content Providers from the link below.

http://developer.android.com/guide/topics/providers/content-providers.html

Note: If i'm not wrong to pass data with content providers between your apps, you need to sign your apps with same keystore.

You can also use Intents to pass data to your application. You can check it from below.

http://developer.android.com/training/sharing/send.html

It's not a good practice but you can use shared preferences.

SharedPreferences preferences = getSharedPreferences(PREF_NAME, MODE_WORLD_READABLE);
savepopulation
  • 11,736
  • 4
  • 55
  • 80
1

A third, and possibly better way would be to use a protocol or custom URI scheme. By default this is the way that Android applications send and receive data. Example, when you click a url link sent to you via email, the app opens the default browser application. This is because the browser registered with the OS that it can handle URI's that match the pattern of a web uri.

See the attached link

http://developer.android.com/training/basics/intents/filters.html

Aiden Strydom
  • 1,198
  • 2
  • 14
  • 43
0

Both applications are yours so can easily make it.

1)By using shared preferences you can save data in mobile from app1 and get that data and use in app2 and vice versa.

SharedPreferences are useful for small data only .

2)Content provider do what you are asking. A content provider is only required if you need to share data between multiple applications

http://developer.android.com/guide/topics/providers/content-providers.html

In your situation you app must be a content provider

refer this what you are asking http://www.compiletimeerror.com/2013/12/content-provider-in-android.html#.Vwsy1vl97IU

Narender
  • 1
  • 2
0

Use Intent to pass data between apps along with BroadCastReceiver

In your appOne Create

1)Dynamic broadcast receiver inside your appOne activity on Particular Event call sendBroadcast(Intent)

Don't call activity as you mentioned above.Register a Broadcast receiver in appTwo with some action and sendBroadacst intent from appOne with same action mentioned in appTwo. So when you fire sendbradcast(Intent) from appOne,appTwo listening for that intent will trigger and there you Call Activity of appTwo(Your MainActivity) Inside onReceive method of appTwo check your activity is Active or not(If activity is active then sendBroadcast(Intent) from appTwo to refresh message in appTwo if not Call Activty)

Now in your appTwo Create

1)Static BroadCastReceiver to receive the intent with some action inside manifest when the app is not open

2)Dynamic broadcast receiver inside your appTwo activity if the app is open to refresh Message

Raghavendra B
  • 441
  • 3
  • 10
0

I solved my problem with this link: http://androiddhamu.blogspot.in/2012/03/share-data-across-application-in.html

appone:

  SharedPreferences prefs = getSharedPreferences("demopref",
                             Context.MODE_WORLD_READABLE);
                     SharedPreferences.Editor editor = prefs.edit();
                     editor.putString("demostring", ""+Connect.MessageArrive.toString());
                     editor.commit();

                  Intent i = new Intent(Intent.ACTION_DATE_CHANGED);
                    PackageManager manager = getPackageManager();
                    i = manager.getLaunchIntentForPackage("com.graphhopper.android");
                   // i.putExtra("MessageText",""+Connect.MessageArrive.toString());
                   i.addCategory(Intent.CATEGORY_LAUNCHER); 


                  //i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    startActivity(i);

apptwo:

  @Override
    public void onResume() {
        super.onResume();
            con = createPackageContext("com.appone.one", 0);
            SharedPreferences pref = con.getSharedPreferences(
                    "demopref", Context.MODE_PRIVATE);
            String data = pref.getString("demostring", "No Value");
}
0

Following things need to be taken care during the implementation.

  1. There are two Apps. App1 and App2. We need to send data from APP1 to APP2 and when APP2 finishes, we need to send Data back from APP2 to APP1.

Steps : -- Add android:exported = "true" in both the Apps manifest for the activities which you want the communication in between.

In App 1

private void launchApp2() {
    Intent intent = new Intent();
    //Need to register your intent filter in App2 in manifest file with same action.
    intent.setAction("com.example.app2.main"); // <packagename.main>
    Bundle bundle = new Bundle();
    bundle.putString("name", "ABC");
    intent.putExtra("data", bundle);
    intent.setType("text/plain");
    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(sendIntent, 101);
    } else {
    Log.d("App not installed")
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 101) {
        if (resultCode == Activity.RESULT_OK) {
            Bundle bundle = data.getBundleExtra("data");
            String username = bundle.getString("name");
            result.success(username);
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

In App2 Manifest,

<activity android:name=".main">
    <intent-filter>    
        <!--The action has to be same as App1-->        
        <action android:name="com.example.app2.main" />
        <category android:name="android.intent.category.DEFAULT" />
        <data android:mimeType="text/plain" />
    </intent-filter>
</activity>

To get the Data in App2,

public getData (){
 Intent intent = new Intent();
 Bundle bundle = intent.getBundleExtra("data");
 String name = bundle.getString("name");
}

To send the data back to App1

public sendData(){
Intent intent = new Intent();
 Bundle bundle = new Bundle();
 bundle.putString("name", "ABC");
 intent.putExtra("data", bundle)
 setResult(Activity.RESULT_OK, intent)
 finish()
}
Anshul Aggarwal
  • 609
  • 6
  • 6
-2

Use Shared Preferences with mode MODE_WORLD_READABLE

SharedPreferences sPref = activity.getPreferences(Activity.MODE_WORLD_READABLE);

Save values in Preference:

Asim Roy
  • 9,915
  • 4
  • 28
  • 40