Although intents facilitate communication between components in several ways, there are three fundamental use cases:
- Starting an Activity .
- Starting a Service .
- Delivering Broadcast .
There are two types of intent
- Implicit Intent .
- Explicit Intent .
Implicit Intent: - Implicit intent do not directly specify the Android components which should be called.
They specify the action which should be performed and optionally an URI which should be used for this action.
Some of the Pr-packaged Application Like :
- Call a number .
- Browse the web for a given URL.
- Sending Email to a given email .
and many more ...
Lets See an Example
Project Structure :
file : build.gradle
apply plugin: 'com.android.application' android { compileSdkVersion 26 defaultConfig { applicationId "com.tutorialsbuzz.implicitintent" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.0' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' }
file : activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp" tools:context="com.tutorialsbuzz.implicitintent.MainActivity"> <Button android:id="@+id/browse_button_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/button_label" android:textSize="22sp" /> </LinearLayout>
file : MainActivity.java
package com.tutorialsbuzz.implicitintent; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = findViewById(R.id.browse_button_id); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Uri uriUrl = Uri.parse("https://www.google.com"); Intent browse_intent = new Intent(Intent.ACTION_VIEW, uriUrl); startActivity(browse_intent); } }); } }
file : AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.tutorialsbuzz.implicitintent"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
RUN:
1 comment:
Small thing, if you create a new folder in your res folder and name it "layout_land", you can create landscape layouts (with the same names as their portrait layout in res/layout) and as he will automatically switch between those two layouts without any additional effort in your java class.
Post a Comment