Passing multiple string arrays between two apps using intent

让人想犯罪 __ 提交于 2019-12-12 02:17:43

问题


I have two separate android apps, AppA and AppB. AppA will launch AppB (which is a game app). After the user is done playing the game (AppB) and click the logout button, it will send the game records (multiple string arrays) back to AppA.

Just like to ask, how can I get AppB to send multiple string arrays to AppA at one go? Thanks.

Eg. AppB is supposed to send these string arrays to AppA:

Game Record :12345,Tan Ah Huay,2x2,living_room,normal,3,100,N,1 Mar 2017 5:11:31 pm,2
Game Record :12345,Tan Ah Huay,2x2,kitchen,normal,2,100,N,1 Mar 2017 5:17:52 pm,1
Game Record :12345,Tan Ah Huay,2x2,living_room,normal,3,100,N,1 Mar 2017 5:18:03 pm,3

But AppA is only receiving the first one:

Game Record :12345,Tan Ah Huay,2x2,kitchen,normal,3,100,N,1 Mar 2017 5:11:20 pm,2

Here are the source codes:

AppA's 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's 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
                }
            });

来源:https://stackoverflow.com/questions/42533566/passing-multiple-string-arrays-between-two-apps-using-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!