Fall 2017 CSE 570 Wireless and Mobile Networks

Spring 2017 ESE 543 & 343 Mobile Cloud Computing

Lab 4: Data Sharing between Apps

Outline of this lab

Warm-up: Share a text with another app. Receive a text from another app

Share any binary data (images) with another app. Receive binary data from another app.

An easy share button

Examples

Send Text Content

Generate an app. Build a new project using the wizard in Android studio, Application name is "DataShare", and company domain is "mcc". Select the Empty Activity. Use default for other settings.

Design a simple layout. In the res/layout/activity_main.xml, include one EditText view and one Button view. For the Button view, define the function name for onclick. The ViewGroup should be LinearLayout

Important: build the intent for data sharing. In MainActivity.java, define the function public void sendData(View v){}. This functions will retrieve input text from the EditText view. An intent will be built. In the intent, the data trype will be set to be "text/plain". (Understand how an intent is built) Specifically, the sendData function is defined as follows.

Receive Text Content

Build a new project using the wizard in Android studio, Application name is "ReceiveData", and company domain is "mcc". Select the Empty Activity. Use default for other settings.

To receive an intent from another application, we need to set up the intent filter in AndroidManifest.xml. For this lab, we will only receive plain text and images. Specifically, the AndroidManifest.xml should be something like this:

Once the intent filter is set up, this app will be invoked when the corresponding intents arrive. The arriving intents can be accessed in the onCreate function of the activity. Next we will implement functions to process the intents. Use the following function to retrieve the intent.

Intentintent=getIntent();

Then we have access to all the information in this intent! We can reference documents of the Intent class for more details of what information we can get. In this lab, we need to get the intent action, intent type, and intent extras. Specifically, we will handle texts and images separately in the onCreate function, as follows:

We will go on to implement the function handleSendText(). This function we retrieve the text received and display it on the screen. The way to do this is to use the function getStringExtra() with the key Intent.EXTRA_TEXT, as shown in the following line:

StringsharedText=intent.getStringExtra(Intent.EXTRA_TEXT);

Then we will display this text on the screen using the TextView class. Specifically, the implementation of handleSendText is as follows:

Download the following two images to the folder "DataShare/DataShare/app/src/main/res/drawable/".

In MainActivity.java, we can easily access these two images by their IDs (R.drawable.i1). However, files in the drawable folder is private and cannot be accessed by another app. We use the MediaStore.Images.Media.insertImage() method to copy i1.jpg and i2.jpg to the media library, and reference them there.

To build and send an intent, we will set the intent action using Intent.setAction() method, Then we will put the list of image uri into the intent, and set the intent type. Finally, we broadcast the intent using the startActivity() method. Specifically, the implementation of public void shareImages(View v) is as follows:

Receive Binary Content

To receive these images, we need to use the method intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM). This can retrieve the list of image uris. Using these uris, we can apply the InputStream class to read the image files. Finally, we a bitmap is constructed using BitmapFactory, and the image is displayed in the screen. The implementation of void handleSendMultipleImages(Intent intent) is as follows.

This will create a menu with a share button. Then we need to implement the logic for the this button. In the MainActivity.java, we need to add these functions:

privateShareActionProvidermShareActionProvider;@OverridepublicbooleanonCreateOptionsMenu(Menumenu){getMenuInflater().inflate(R.menu.share_menu,menu);// Retrieve the share menu itemMenuItemshareItem=menu.findItem(R.id.menu_share);// Now get the ShareActionProvider from the itemmShareActionProvider=(android.support.v7.widget.ShareActionProvider)MenuItemCompat.getActionProvider(shareItem);setShareIntent(createShareIntent());returnsuper.onCreateOptionsMenu(menu);}privateIntentcreateShareIntent(){IntentshareIntent=newIntent(Intent.ACTION_SEND);shareIntent.setType("text/plain");shareIntent.putExtra(Intent.EXTRA_TEXT,"edit the text you want to share");returnshareIntent;}privatevoidsetShareIntent(IntentshareIntent){if(mShareActionProvider!=null){mShareActionProvider.setShareIntent(shareIntent);}}

Besides, change the MainActivity class to be extending ActionBarActivity.

publicclassMainActivityextendsActionBarActivity

In this case, pay special attention to what packages you import. There could be a lot of errors coming from package imports.

Lab 4 Homework

Build an app that can select any image in the external storage. Share this image with another app and be able to display.