Share & Call functions [Bagian 10]

Secure! for those of you who have followed this tutorial up to now. Because you’ve successfully created a mobile GIS application using Android Studio. In this tutorial, there is just one sweet thing about the application being built, which is adding features share Detailed information about the music studio and also Call Contact the studio to make a booking or ask for information about the music studio.

For those of you who are new to this tutorial, you can start with the first tutorial which you can see at the link below:

For those of you who have completed the previous tutorials, here are the steps you can take to add functionality share and Call for detailed music studio information:

1. Open the previous project, make sure you can view the marker and double click will show the detailed layout of the music studio information.

2. When there are no bugs and is going well. open minded DetailStudio.java, add the script below onCreate :

Define the data types for the following variables beforehand:

    private ImageView callPhone, callShare;    public static Double latitude, longitude;
        callPhone = (ImageView) findViewById(R.id.call_button);        callPhone.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                AlertDialog.Builder builder = new AlertDialog.Builder(DetailStudio.this);                builder.setMessage("Call " + callSave + " ?");                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Intent i = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + callSave));                        try {                            startActivity(i);                        } catch (SecurityException se) {                            Toast.makeText(DetailStudio.this, "Permission Denied", Toast.LENGTH_SHORT).show();                        }                    }                });                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                    }                });                AlertDialog alert = builder.create();                alert.show();            }        });        //Button SHARE        callShare = (ImageView) findViewById(R.id.share_button);        callShare.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);                sharingIntent.setType("text/plain");                String shareBody = namaStudio + "nn"                        + " "                        + "Alamat:" + "n" + alamat + "nn"                        + " "                        + "Jam Operasional:" + "n" + jam + "nn"                        + " "                        + "Harga:" + "n" + harga + "nn"                        + " "                        + "Alat Musik:" + "n" + alatmusik + "nn"                        + " "                        + "No Telepon:" + "n" + callSave + "nn"                        + " "                        + "Update Terakhir:" + "n" + lastUpdate + "nn"                        + " "                        + "Lokasi Studio Musik:" + "n" +                        "http://maps.google.com/?q=" + latitude + "," + longitude + "nn"                        + " "                        + "'Find Your Music Studio!'";                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);                startActivity(Intent.createChooser(sharingIntent, "Pilih Aplikasi"));            }        });
  • The above Intent command is used to display Phone call on the user’s Android smartphone by calling the phone number in the callSave variable. Where the variable already contains data that will be sent when the user clicks on the selected studio marker.

In the script above, first define id Call button to xml as a variable Call phone, then variable Call phone function used setOnClickListener in which there is a method onClick with an instruction that a. indicates AlertDialog, where is positive and negative button.

to positive button appears with the text “YES”, which executes the command when selected intention including:

Intent i = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + callSave));

And to Negative button no command is given, so the display remains in the Detail Studio

  • For the share function it is then the same as for the call function. First define the share_button ID, which is in the XML, for the callShare variable. Then given the setOnClickListener function which contains the following intent command:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);sharingIntent.setType("text/plain");

command Intention.ACTION_SEND Above is used to send a message with text, picture or sound to applications such as WhatsApp, Line, SMS, Email, etc., which are already installed on the user’s Android smartphone. Then for the type use “Text / simple” This means that only information is provided in the form of text. Then for shareBody Simply choose to share all information by calling variables from each information such as name, address, phone number, etc.

To see the selection of applications you want to share, you can use the command Intent.createChooser as above.

3. To see if the script you added works, just run the program. If the output looks like this, it means that the sharing and calling features can be used:

Call functions

Share function

In the author’s emulator there are no share applications like Whatsapp, Line, Facebook, etc., so the program goes straight to SMS to be able to send information.

This completes the creation of detailed information from the music studio. In the next tutorial we will discuss how to add features favourite with database sqllite. I’m looking forward to the next tutorial …






Leave a Reply

Your email address will not be published.