วรเศรษฐ สุว รรณิก - WordPress.com

knapsackyarnMobile - Wireless

Dec 14, 2013 (3 years and 4 days ago)

179 views

1
วรเศรษฐ สุวรรณิก
uuriter@yahoo.com
http://bit.ly/WannikAcademy
โค้ดส่วนหนึ่งเขียนโดย อ
.
ทศพล ธนะทิพานนท์
thanatipanonda@gmail.com
Android Programming
2
Intent

Implicit

Explicit

Registered Activity
3
Implicit Intent

open a web page

make a phone call

display a map

send an email

open a contact list

getting an image from a gallery

taking a picture
4
Open a web page
Uri uri = Uri.
parse
(
"http://www.wannik.com/"
);
Intent intent =
new
Intent(Intent.
ACTION_VIEW
, uri);
startActivity(intent);
5
Make a phone call
Uri uri = Uri.
parse
(
"tel:0867874846"
);
Intent intent =
new
Intent(Intent.
ACTION_CALL
, uri);
startActivity(intent);
<uses-permission android:name="android.permission.CALL_PHONE" />
6
Display a map
Uri uri = Uri.
parse
(
"geo:13.8453,100.5723?z=15"
);
Intent intent =
new
Intent(Intent.
ACTION_VIEW
, uri);
startActivity(intent);
ถ้าต้องการสร้าง
activity
ที่แสดงแผนที่เองโดยไม่ใช้โปรแกรม
Map
ให้สร้างคลาสที่สืบทอดจาก
MapActivity
และ ใช้
LocationListener
เพื่อฟังเหตุการณ์พิกัดเปลี่ยน
onLocationChanged
7
Open a contact list
Uri uri = Uri.
parse
(
"content://contacts/people/"
);
Intent intent =
new
Intent(Intent.
ACTION_VIEW
, uri);
startActivity(intent);
8
Send an email
Intent intent =
new
Intent(Intent.
ACTION_SEND
);
String[] to = {
"uuriter@yahoo.com"
};
intent.putExtra(android.content.Intent.
EXTRA_EMAIL
, to);
intent.putExtra(android.content.Intent.
EXTRA_SUBJECT
,
"hi"
);
intent.putExtra(android.content.Intent.
EXTRA_TEXT
,
"hello world"
);
intent.setType(
"text/plain"
);
startActivity(intent);
9
Getting an image from a gallery

ถ้าใช้
emulator

Push an image to /mnt/sdcard

Restart

ในขั้นตอนนี้ถ้าทำไม่ได้ แสดงว่าไม่ได้จองพื้นที่ของ
sdcard
ไว้ตอนสร้าง
AVD
10
Getting an image from a gallery
public

void
click(View view){

Intent galleryIntent =
new
Intent(Intent.
ACTION_PICK
,

Media.
EXTERNAL_CONTENT_URI
);

startActivityForResult(galleryIntent,
0
);
}
@Override
public

void
onActivityResult(
int
requestCode,
int
resultCode, Intent
data
) {

Uri imageUri =
data
.getData();

// Log.
i
(
"intent"
, imageUri.toString());

ImageView
imageView1 = (ImageView) findViewById(R.id.
imageView1
);

imageView1.setImageURI(imageUri);
}
11
Taking a picture
public

void
click(View view) {

String path = Environment.
getExternalStorageDirectory
().getAbsolutePath()

+
"/pic.jpg"
;

Uri imageUri = Uri.
fromFile
(
new
File(path));

Intent intent =
new
Intent(MediaStore.
ACTION_IMAGE_CAPTURE
);

intent.putExtra(MediaStore.
EXTRA_OUTPUT
, imageUri);

startActivityForResult(intent, 0);
}
// same as the previous code
@Override
protected

void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {

Uri imageUri = data.getData();

// Log.
i
(
"intent"
, imageUri.toString());

ImageView imageView1 = (ImageView) findViewById(R.id.
imageView1
);

imageView1.setImageURI(imageUri);
}
12
Taking a picture
13
Explicit Intent

just call

send data

receive result
14
Just Call

New > Other...

Android > Activity

+xml, +java, manifest file

public

class
MainActivity
extends
Activity {

public

void
clk(View view) {

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

startActivity(intent);

}
15
Send data
// MainActivity sends data
Intent intent =
new
Intent(
this
,
AnotherActivity
.
class
);
intent.putExtra(
"name"
,
"John"
);
startActivity(intent);
// AnotherActivity receives data
Intent intent = getIntent();
String name = intent.getStringExtra(
"name"
);
16
Receive Result
// MainActivity
Intent intent =
new
Intent(
this
,
AnotherActivity
.
class
);
startActivityForResult(intent, 1234);
//AnotherActivity
Intent intent =
new
Intent();
intent.putExtra(
"k"
,
"This is a result"
);
setResult(
RESULT_OK
, intent);
finish();
// MainActivity
@Override
protected

void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {

String msg = data.getStringExtra(
"msg"
);


Toast.
makeText
(
this
, msg, Toast.
LENGTH_SHORT
).show();
}
17
Receive Result
// MainActivity
Intent intent =
new
Intent(
this
,
AnotherActivity
.
class
);
startActivityForResult(intent, 1234);
//AnotherActivity
Intent intent =
new
Intent();
intent.putExtra(
"k"
,
"This is a result"
);
setResult(
RESULT_OK
, intent);
finish();
// MainActivity
@Override
protected

void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {

if
(resultCode == Activity.
RESULT_OK
) {

if
(requestCode == 1234) {

String msg = data.getStringExtra(
"msg"
);

Toast.
makeText
(
this
, msg, Toast.
LENGTH_SHORT
).show();

}

}
18
Send/Receive
19
Registered Activity

Intent filter
20
FireFish
21
FireFish Activity
@Override
protected

void
onCreate(Bundle savedInstanceState) {

super
.onCreate(savedInstanceState);

setContentView(R.layout.
activity_main
);

Intent
intent
= getIntent();

Uri uri = getIntent().getData();

if
(uri !=
null
) {

TextView textView1 = (TextView) findViewById(R.id.
textView1
);

// textView1.setText(intent.getDataString());

textView1.setText(uri.getHost());

}
}
22
FireFish Manifest
<?
xml

version
=
"1.0"

encoding
=
"utf-8"
?>
<
manifest
...
>

...

<
application

android:allowBackup
=
"true"

android:icon
=
"@drawable/ic_launcher"

android:label
=
"@string/app_name"

android:theme
=
"@style/AppTheme"

>

<
activity

android:name
=
"com.example.testintent2.MainActivity"

android:label
=
"
FireFish
"

>

<
intent-filter
>

<
action

android:name
=
"android.intent.action.MAIN"

/>

<
category

android:name
=
"android.intent.category.LAUNCHER"

/>

</
intent-filter
>

<
intent-filter
>

<
action

android:name
=
"android.intent.action.VIEW"

/>

<
category

android:name
=
"android.intent.category.DEFAULT"

/>

<
data

android:scheme
=
"http"

/>

</
intent-filter
>

</
activity
>

</
application
>
</
manifest
>
23
Caller's Activity
public

class
MainActivity
extends
Activity {

@Override

protected

void
onCreate(Bundle savedInstanceState) {

super
.onCreate(savedInstanceState);

setContentView(R.layout.
activity_main
);

}


public

void
clk(View view) {

Uri uri = Uri.
parse
(
"http://wannik.com"
);

Intent intent =
new
Intent(Intent.
ACTION_VIEW
, uri);

startActivity(intent);

}
24
tel:0811111111
ssp
25
tel:0811111111
ssp