Sending array with Intent to different package

2019-08-29 02:20发布

问题:

I have problem with an Intent for an array. I want to send an rectangular array to another package. I'm following this tutorial. Unfortunately, I still have a problem with the Intent. So I have the class ActivityToSend. I want to send storePrintIMC into another class (SamplePreferenceActivity) of a different package.

ActivityToSend class:

public class ActivityToSend extends Activity {
    private Activity activity;
    private ArrayList<String[]> data;
    private static LayoutInflater inflater = null;
    public String[] inkLevels = new String[5];
    View message;
    String [][] storePrintIMC= new String [4][4];
    public static final String ARRAYS_COUNT = "com.Status.App.ARRAYS_COUNT";
    public static final String ARRAY_INDEX = "com.Status.App.ARRAY_INDEX"; 

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        final String data[][] = storePrintIMC; 
        Bundle bundle = new Bundle();
        int count = data.length; 
        bundle.putInt(ARRAYS_COUNT, count); 
        for (int i = 0; i < count; i++)
        { 
            bundle.putStringArray(ARRAY_INDEX + i, data[i]); 
        } 
        Intent intent = new Intent(this, SamplePreferenceActivity.class); 
        intent.putExtras(bundle);

        startActivity(intent);
    }
}

For the second activity in the different package I use:

public class SamplePreferenceActivity extends PreferenceActivity {    
    private static final int DIALOG_READ_ME = 1;
    public String[] inkLevels = new String[5];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            int count = bundle.getInt(ActivityToSend.ARRAYS_COUNT, 0);

            ArrayList<String[]> arrays = new ArrayList<String[]>(count);

            for (int i = 0; i < count; i++)
                arrays.add(bundle.getStringArray(ActivityToSmartWatch.ARRAY_INDEX + i));

            String[][] data = arrays.toArray(new String[][]{});
        }
    }
}

I also made some changes with the Android manifest. For the second package, I put:

<activity android:name=".SamplePreferenceActivity " android:label="@string/preference_activity_title">
<meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.PrinterStatus.AppLab.SamplePreferenceActivity " /> 
  - <intent-filter>
        <action android:name="android.intent.action.MAIN" /> 
    </intent-filter>
</activity>
<activity android:name="com.sonyericsson.extras.liveware.extension.controlsample.SamplePreferenceActivity" android:parentActivityName="com.PrinterStatus.AppLab.ActivityToSend" /> 
<service android:name="com.sonyericsson.extras.liveware.extension.controlsample.SampleExtensionService" /> 

I get an error in the first package in:

Intent intent = new Intent(this, SamplePreferenceActivity.class);

I don't know what I have to change. Do you have any idea?

回答1:

Since SamplePreferenceActivity is in a different package, you need to import the class into ActivityToSend. It's just like any other import. Judging from what's in your manifest, you need to put this near the top of com.PrinterStatus.AppLab.ActivityToSend.java (with the other import statements):

import com.sonyericsson.extras.liveware.extension.controlsample.SamplePreferenceActivity;

If you're using Eclipse, you can probably resolve the import automatically using ctrl-shift-O.