Hello. I'm Wataku, a server-side programmer who is studying programming at a certain school. : relaxed: Let's develop Android this time as well. The theme this time is * "Intent" *.
--A person who can write Java somehow. --Someone who is ambiguous in Android development but can do it a little.
--Explicit intent → Specify the activity class to start.
--Implicit intent → Specify the URI and action (described later) that indicate what kind of activity you want to launch. The Android OS finds and launches the corresponding activity based on the URI and action. If there are multiple launch destinations, the app chooser will be displayed.
Now let's see how to handle it.
To make a screen transition with an explicit intent: ** 1) Register the activity in AndroidManifest.xml **
<?xml version="1.0" encoding="utf-8"?>
<manifest ...... >
<application ........ >
<activity>
~ Main activity ~
</activity>
<!--add to-->
<activity android:name="Package name + class name">
</application>
</manifest>
*If the activity class is directly under the root package, "."Class name" is OK!
** It is convenient if you create the above procedure from "File> New> Activity> Empty Activity". *
** 2) Start screen ** ○ How to start another screen from the current screen. (1) Intent object generation.
~~~~~~~~ = new Intent(context,Launched activity)
(2) Execute the ** startActivity () ** method with (1) as an argument.
** 3) Data passing ** ○ Use ** putExtra ("name", value) ** to pass data to the startup activity.
** 4) Receive data ** ○ The method of receiving data in the startup activity is as follows. ① Get Intent object
Intent intent = getIntent();
② Get Bundle object
Bundle extra = intent.getExtra();
③ Get data using get data type method of Bundle ForResult ○ When processing with the original activity after the startup activity ends ** 1) The following method to start the activity **
startActivityForResult(Intent object,Request code)
** 2) The following method immediately before finish () of the startup activity **
setResult(Result code,Intent object)
** Result code *
** 3) Execute 1) and 2) and execute the following method in the original activity. ** **
onActivityResult()
3 arguments
--int requestCode: 1) integer value specified by the second argument --Int resultCode: 2) constant value specified by the first argument
ForResultSampleActivity.java(Launch source)
Intent intent = new Intent(ForResultSampleActivity.this,RatingEvaluateActivity.class);
inten1.putExtra("name", name);
startActivityForResult(intent, RATING_EVALUATE);
RatingEvaluateActivity.java(Startup destination)
Intent intent = getIntent();
String name = intent.getStringExtra("name");
The procedure for launching other apps with an implicit intent is as follows. ** 1) Create a URI object. ** **
Uri uri = Uri.porse(URI string(See below));
** 2) Create an Intent object. ** **
Intent intent = new Intent(A constant that represents an action.(See below), uri);
** 3) Launch the activity. ** **
startActivity(intent) ;
URI The URI of the Android OS standard application is as follows.
・ Browser → http: // ....., https: // ..... ・ Map → geo: Latitude, longitude geo: o, o? g = search string
(note)
Japanese search keyword is Encode with URLEncoder.encode ([keyword], [encode format (URF-8, etc.)]).
・ Telephone → tel: Phone number
Use the constant field of the Intent class.
--ACTION_VIEW → Screen display --ACTION_CALL → Make a call based on the data. --ACTION_DIAL → Display the screen to make a call --ACTION_SEND → Send email / SMS
① Transition to the map application
try {
TextView etKeyword = findViewById(R.id.etKeyword);
String keyword = etKeyword.getText().toString();
keyword = URLEncoder.encode(keyword, "utf-8");
Uri uri = Uri.parse("geo:0,0?q=" + keyword);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
} catch(UnsupportedEncodingException ex) {
Log.e("MapSearchActivity", "keyword conversion failure", ex);
}
② Transition to the browser
String url = "http:://www.~~~.~~";
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
that's all. If you have any suggestions such as something wrong, please contact us. Thank you for reading to the end.
Recommended Posts