In Android development we often use intents to use Activity Communications and other Intent Services. As per Documentation "An Intent is a messaging object you can use to request an action from another app component ".
Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc.
It is generally used with startActivity() method to invoke activity, broadcast receivers etc.
The dictionary meaning of intent is intention or purpose. So, it can be described as the intention to do action.
Android intents are mainly used to:
- Start the service
- Launch an activity
- Display a web page
- Display a list of contacts
- Broadcast a message
- Dial a phone call etc.
There are two types of intents in android:
1) Implicit Intent:
Implicit Intent doesn't specifiy the component. In such case, intent provides information of available components provided by the system that is to be invoked.
For example, you may write the following code to view the webpage.
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.droidmedium.blogspot.com"));
startActivity(intent);
2) Explicit Intent:
Explicit Intent specifies the component. In such case, intent provides the external class to be invoked.
Intent i = new Intent(getApplicationContext(), OtherActivity.class);
startActivity(i);
So Above is brief introduction of intent. Lets use Practical example of different types of explicit intnt examples:
Intent to Open Web Browser for specific URL:
String url = "http://www.droidmedium.blogspot.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
Intent to Open Email Client:
Intent mEmail=new Intent(Intent.ACTION_SENDTO);
mEmail.setData(Uri.parse("mailto: example@gmail.com"));
mEmail.putExtra(Intent.EXTRA_EMAIL, new String[]{"example@gmail.com"});
mEmail.putExtra(Intent.EXTRA_SUBJECT,"Your Subject Here");
mEmail.putExtra(Intent.EXTRA_TEXT, "Your Message Here");
startActivity(Intent.createChooser(mEmail, "Choose An Email Client to send your feedback!!"));
Intent to Open Camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, TAKE_PICTURE);
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
Intent to Open Contact:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
Intent to Open PDF File:
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/example.pdf");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
Intent to Open Google Map:
Intent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("http://maps.google.com/maps?saddr=xx.xxxx,yy.yyyy&daddr=xx.xxxxxx,yy.yyyyyy"));
startActivity(intent);
Intent to Open WhatsApp:
String contact = "+91 xxxxxxxxxx"; // use country code with your phone number
String url = "https://api.whatsapp.com/send?phone=" + contact;
try {
PackageManager pm = context.getPackageManager();
pm.getPackageInfo("com.whatsapp", PackageManager.GET_ACTIVITIES);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
Toast.makeText(MainActivity.activity, "Whatsapp app not installed in your phone", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
Intent to Open Play Store:
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
Intent to Open Another App:
// Use package name which we want to check
boolean isAppInstalled = appInstalledOrNot("com.check.application");
if(isAppInstalled) {
//This intent will help you to launch if the package is already installed
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("com.check.application");
startActivity(LaunchIntent);
Log.i("Application is already installed.");
} else {
// Do whatever we want to do if application not installed
// For example, Redirect to play store
Log.i("Application is not currently installed.");
}
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
}
return false;
}
Intent to Open Apk Installer:
Uri uri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider",new File(location+"Example.apk")); // location is path of file location
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
getContext().startActivity(intent);
Intent to Open SMS App:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_MESSAGING);
startActivity(intent);
Intent to Ope UPI Apps:
String payeeAddress = "xxxxxxxxx@upi"; // replace x with payee UPI Address
String payeeName = "Droid Medium"; // replace with payee Name
String transactionNote = "Test for UPI Deeplinking"; //can pass random txn Id
String amount = "1"; // Amount here
String currencyUnit = "INR";
String transid ="ABX1234"; // Random transaction Id
Uri uri = Uri.parse("upi://pay?pa="+payeeAddress
+"&pn="+payeeName
+"&tn="+transactionNote
+ "&mc=0000"
+"&tid=" + transid
+ "&tr=" + transid
+"&am="+amount
+"&mam=" + null
+"&cu="+currencyUnit);
Log.d(TAG, "onClick: uri: "+uri);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivityForResult(intent,1);
Thats all for now .So using these example we can use implicit Intent for our Android App.
References: StackOverflow & JavaTpoint
Comments
Post a Comment