user3747512
user3747512

Reputation: 223

How to pass data from one app to another in android

I have developed 2 apps, one for receiving data and displaying a list. Through this list I get the data which I need to pass to another app to plot the data received by showing a Pie Chart.

Through my first app, the code snippet is below, I pass the data that is the double variable "pie" through Actionbar Click to open my second app.

public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

case R.id.action_settings:
            if(item.isChecked()) item.setChecked(false);
            else item.setChecked(true);
            Intent i;
            PackageManager manager = getPackageManager();
            try {
                i = manager.getLaunchIntentForPackage("com.example.piechartexample");
                if (i == null)
                    throw new PackageManager.NameNotFoundException();
                i.addCategory(Intent.CATEGORY_LAUNCHER);
                i.setAction("android.intent.action.MAIN");
                i.putExtra("Send", pie );
                startActivity(i);
            } catch (PackageManager.NameNotFoundException e) {

            }
            return true;
default: x="";Log.d(TAG, x);
            return super.onOptionsItemSelected(item);
    }

This opens my second app piechartexample. But I am not able to get the value of pie to my second app. Code snippet of my second app below

public class MainActivity extends Activity { 

private  double[] VALUES =getIntent().getDoubleArrayExtra("Send"); 

// some other variables 
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pie_chart_main);
// show pie char using achartengine
}
}

the error from logcat is

D/AndroidRuntime(21366): Shutting down VM
W/dalvikvm(21366): threadid=1: thread exiting with uncaught exception (group=0x41624ba8)
E/AndroidRuntime(21366): FATAL EXCEPTION: main
E/AndroidRuntime(21366): Process: com.example.piechartexample, PID: 21366
E/AndroidRuntime(21366): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.piechartexample/com.example.piechartexample.MainActivity}: java.lang.NullPointerException
E/AndroidRuntime(21366):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
E/AndroidRuntime(21366):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
E/AndroidRuntime(21366):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
E/AndroidRuntime(21366):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
E/AndroidRuntime(21366):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(21366):    at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(21366):    at android.app.ActivityThread.main(ActivityThread.java:5001)
E/AndroidRuntime(21366):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(21366):    at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(21366):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
E/AndroidRuntime(21366):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
E/AndroidRuntime(21366):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(21366): Caused by: java.lang.NullPointerException
E/AndroidRuntime(21366):    at com.example.piechartexample.MainActivity.<init>(MainActivity.java:30)
E/AndroidRuntime(21366):    at java.lang.Class.newInstanceImpl(Native Method)
E/AndroidRuntime(21366):    at java.lang.Class.newInstance(Class.java:1208)
E/AndroidRuntime(21366):    at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
E/AndroidRuntime(21366):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
E/AndroidRuntime(21366):    ... 11 more

Upvotes: 0

Views: 2584

Answers (3)

Anirudh Sharma
Anirudh Sharma

Reputation: 7974

well if you want to send data from one app to another you can use content provider. the following links may help you.Try this and reply if it works

http://developer.android.com/guide/topics/providers/content-providers.html http://www.tutorialspoint.com/android/android_content_providers.htm

Upvotes: 0

carterh062
carterh062

Reputation: 190

You should be able to just call Intent.getStringExtra ("Send"); in the next activity. I assumed that pie is a string in this case.

Call that getdoubleArrayExtra() in the onCreate(Bundle savedInstance) method like

public class MainActivity extends Activity {

// some other variables 
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pie_chart_main);
    private  double[] VALUES =getIntent().getDoubleArrayExtra("Send"); 
    // show pie char using achartengine
}
}

Upvotes: 1

Michel_T.
Michel_T.

Reputation: 2821

In the receiving activity:

Bundle extras = getIntent().getExtras(); 
String send;

if (extras != null) {
    send = extras.getString("Send");
}

Upvotes: 1

Related Questions