암시적 인텐트(Implicit Intent)


activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.implicitintent.mystoryg.implicitintent.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dial"
android:text="@string/call_dial"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/map"
android:text="@string/call_map"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/url"
android:text="@string/call_url"/>

</LinearLayout>


MainActivity.java

 package com.implicitintent.mystoryg.implicitintent;


import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener {

static final int[] BUTTONS = {
R.id.dial,
R.id.map,
R.id.url
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

for (int buttons : BUTTONS) {
Button button = (Button) findViewById(buttons);
button.setOnClickListener(this);
}
}

@Override
public void onClick(View v) {
int id = v.getId();
Intent intent;
switch (id) {
case R.id.dial:
intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:02-1588-3820"));
startActivity(intent);
break;
case R.id.map:
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("geo:37.586644,126.974755"));
startActivity(intent);
break;
case R.id.url:
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);
break;
}
}
}




+ Recent posts