[英]How to pass data from one app to another in android
我已經開發了2個應用程序,其中一個用於接收數據並顯示列表。 通過此列表,我獲得了需要傳遞到另一個應用程序以顯示餅圖來繪制接收到的數據的數據。
通過我的第一個應用程序,下面的代碼段,我通過Actionbar單擊傳遞了雙變量“ pie”的數據,以打開第二個應用程序。
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);
}
這將打開我的第二個應用程序piechartexample。 但是我無法在第二個應用程序中獲得派的價值。 我下面的第二個應用程序的代碼段
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
}
}
logcat的錯誤是
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
您應該可以只調用Intent.getStringExtra ("Send");
在下一個活動中。 在這種情況下,我假設pie
是一個string
。
在onCreate(Bundle savedInstance)
方法中調用getdoubleArrayExtra()
,例如
公共類MainActivity擴展了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
}
}
在接收活動中:
Bundle extras = getIntent().getExtras();
String send;
if (extras != null) {
send = extras.getString("Send");
}
如果您想將數據從一個應用程序發送到另一個應用程序,則可以使用內容提供程序。 以下鏈接可能會對您有所幫助。
http://developer.android.com/guide/topics/providers/content-providers.html http://www.tutorialspoint.com/android/android_content_providers.htm
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.