Explicit and Implicit intents. �Sending and receiving data, using intent-filter. �Transferring data through Bundle.
�An Intent in Android is a message object used to start another Activity, Service, or BroadcastReceiver and to pass data between them.��
Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
intent.putExtra("username", "Bobur");
startActivity(intent);
In HomeActivity :
String name = getIntent().getStringExtra("username");
Explicit Intent
Used when you want to open a specific Activity within your own app.
Implicit Intent (Noaniq Intent)����
Example: Opening a browser with a link.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://google.com"));
startActivity(intent);
The system will display apps like Chrome, Firefox, etc.
2. Intent-filter��
<activity android:name=".WebActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="http" />
<data android:scheme="https" />
</intent-filter>
</activity>
Now, if an intent with ACTION_VIEW and a https://... URL is sent, your WebActivity will appear as a choice.
Another Example: SMS Sending with Intent-filter�Manifest
<activity android:name=".SendSmsActivity">
<intent-filter>
<!-- Amal turi -->
<action android:name="android.intent.action.SENDTO" />
<!-- Kategoriya -->
<category android:name="android.intent.category.DEFAULT" />
<!-- Faqat sms va smsto sxemalarini qabul qilamiz -->
<data android:scheme="smsto" />
<data android:scheme="sms" />
</intent-filter>
</activity>
// Intent orqali kelayotgan ma'lumotni olish
Intent intent = getIntent();
Uri data = intent.getData();
// masalan: smsto:+998901234567
TextView tv = findViewById(R.id.textView);
if (data != null) {
tv.setText("SMS yuboriladigan raqam: " + data.getSchemeSpecificPart());
}
How does it work?�If such an Intent is sent from another place (for example, from a browser or another app):
Intent intent = new Intent(Intent.ACTION_SENDTO); intent.setData(Uri.parse("smsto:+998901234567")); intent.putExtra("sms_body", "Salom! Qalaysan?");
startActivity(intent);
Android tizimi sizga SMS yuborish uchun tanlov beradi: standart Messages app yoki sizning SendSmsActivity.
3. Sending and Receiving Data (putExtra, getExtra)�Sending a simple key-value pair:�
// Yuborish
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("age", 22);
startActivity(intent);
// Qabul qilish
int age = getIntent().getIntExtra("age", 0);
A Bundle is like a container (key–value storage) used to pass multiple pieces of data together between Activities, Fragments, or Services.
// Sending
Bundle bundle = new Bundle();
bundle.putString("username", "Bobur");
bundle.putInt("score", 95);
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtras(bundle);
startActivity(intent);
// Receiving
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String user = bundle.getString("username");
int score = bundle.getInt("score");
}
Example: Choosing photo from Galery
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, 1);
If user choose an image from Galaery, you’ be returned Uri.
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///sdcard/music.mp3"), "audio/*");
startActivity(intent);
Android requests you to choose which app to open mp3 file.