Mobile Computing: Intents
Mobile Computing: Intents
Mobile Computing: Intents
Lecture #05
Intents
Previous Lecture
Android Activities
Today’s Lecture
Intents
Intent Usage
1. Start an Activity
2. Start a Service
3. Delivering a Broadcast
Intent Types
1. Explicit Intents
2. Implicit Intents
Creating an Intent
Receiving an Implicit Intent
Pending Intent
Using a Pending Intent
Intent Resolution
Activity
Activities are subclasses of the Activity class.
By default, when you create a new project, Your activities are
subclasses of the AppCompatActivity class. The AppCompatActivity
class is a subclass of Activity that lets you to use up-to-data Android
app features .
myActivity.class
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
}
Intent
An Intent is a small message passed around the Android system
One can simply say that it is a abstract description of an operation to be
performed
Operation may include starting an activity, launching a service, delivering a
broadcast, setting alarm and triggering an event in future etc.
Intent can use class name (package name) or a URI as target component
In case, the intent wants to launch an activity or a service, target must be listed
in manifest.xml file
In case of URI, then the intent can mention attributes (the scheme, host name, or
MIME type)
Similarly, the intent handling component must match the attributes of URI
Intent Usage … Start an Activity
An Activity represents a screen in an Android application
An Activity (say MyActivity) can start a new Activity (say AnotherActivity)
using startActivity() method
startActivity() method requires an Intent as its parameter
The Intent contains information about which Activity to start and necessary
data
If current Activity requires that launching Activity must return some result,
startActivityForResult() should be called instead of startActivity()
Launching activity will return the result that current activity receives in a
separate Intent object in its onActivityResult() callback
Intent Usage … Start an Activity … Launch Code
Intent intent = new Intent(MainActivity.this, NextActivity.class);
startActivity(intent);
Intent Usage … Start an Activity … AndroidManifest
<manifest xmlns:android=http://schemas.android.com/apk/res/android package="com.imran.first">
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NextActivity"></activity>
</application>
</manifest>
Intent Usage … Start an Activity … activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/next“
android:onClick=“launchNextActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Intent Usage … Start an Activity … next.xml
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
tools:context=".NextActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/back"
android:textSize="20dp"
android:textColor="@color/grey"
android:onClick="goBack"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Intent Usage … Start an Activity … MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void launchNextActivity(View view) {
Intent intent = new Intent(MainActivity.this, NextActivity.class);
startActivity(intent);
}
}
Intent Usage … Start an Activity … NextActivity.java
public class NextActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
}
public void goBack(View view){
Intent intent = new Intent(NextActivity.this,
MainActivity.class);
startActivity(intent);
finish();
}
}
Intent Usage … Start Activity for Result… activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="20dp"
android:text="Result will be shown here" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/result"
android:layout_centerHorizontal="true"
android:onClick="launchDataActivity"
android:text="@string/click" />
</RelativeLayout>
Intent Usage … Start Activity for Result… data.xml
Intent Usage … Start Activity for Result… data.xml
Intent Usage … Start Activity for Result… MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = findViewById(R.id.result);
}
}
Intent Usage … Start Activity for Result… Utils.java
public class Utils {
public static final int REQUEST_CODE = 0x9344;
}
Intent Usage … Start an Service
Just like activities, a service (background running process) can also be
launched using an intent