使用intent在两个应用程序之间传递多个字符串数组

时间:2017-03-01 13:51:39

标签: java android android-intent android-sqlite

我有两个独立的Android应用程序,AppA和AppB。 AppA将推出AppB(这是一款游戏应用)。用户完成游戏(AppB)并单击注销按钮后,它会将游戏记录(多个字符串数组)发送回AppA。

就像问一下,我怎样才能让AppB一次性向AppA发送多个字符串数组 ?感谢。

EG。 AppB应该将这些字符串数组发送到AppA:

  

比赛纪录:12345,Tan Ah Huay,2x2,living_room,normal,3,100,N,2017年3月1日下午5:11:31,2   
游戏记录:12345,Tan Ah Huay,2x2,厨房,正常,2,100,N,2017年3月1日5:17:52 pm,1   
游戏记录:12345,Tan Ah Huay,2x2,living_room,normal,3,100,N,2017年3月1日下午5:18:03,3

但AppA只收到第一个:

  

比赛记录:12345,Tan Ah Huay,2x2,厨房,正常,3,100,N,2017年3月1日下午5:11:20,

以下是源代码:

AppA的StartGameActivity:

//To launch AppB game app
Intent launchGameIntent = getPackageManager().getLaunchIntentForPackage("com.joy.AppB");
startActivity(launchGameIntent);

//Retrieving game records from AppB game
Intent intent = getIntent();
String[] gameRecords_array = intent.getStringArrayExtra("gameRecord");

AppB的MainActivity:

logout_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AlertDialog.Builder logout_alert = new AlertDialog.Builder(MainActivity.this);
            logout_alert.setMessage(getLanguageText("logout_msg"));
            logout_alert.setTitle(getLanguageText("logout"));
            logout_alert.setPositiveButton(getLanguageText("yes"), new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    database = new DatabaseHelper(MainActivity.this.getApplicationContext()).getReadableDatabase();

                    String query = "SELECT * FROM gameRecord";
                    Cursor mcursor = database.rawQuery(query, null);
                    int counter = mcursor.getCount();   // Find out the number of records found in database
                    System.out.println("Number of record : " + counter);

                    String[] gameRecord_array = new String[counter];
                    int array_ptr = 0;

                    mcursor.moveToFirst();
                    if (mcursor != null) {
                        if (mcursor.moveToFirst()) {
                            do {

                                String record = mcursor.getString(mcursor.getColumnIndex("record"));
                                gameRecord_array[array_ptr] = record;
                                array_ptr++;

                            } while (mcursor.moveToNext());

                            // Print out all the game record
                            for (int u = 0; u < gameRecord_array.length; u++) {
                                System.out.println("Game Record :" + gameRecord_array[u]);
                            }

                            //Pass game record to AppA
                                Intent i = new Intent();
                                i.setAction("com.joy.AppA.views.activities.LAUNCH_IT");
                                i.putExtra("gameRecord", gameRecord_array);
                                startActivity(i);
                                System.out.println("Game Record sent to AppA :" + gameRecord_array);
                        }
                    }
                    database.close();
                    finish(); //End AppB
                }
            });

0 个答案:

没有答案