Quite often in Android you need to call a 3rd party Intent or activity.
For example, to scan a barcode using the Barcode Scanner application, or to start the "hidden" Field Test application in HTC phones.
In order to make the life of a user easier, it is a good idea to check if the Intent Receiver/Activity exists before you attempt to invoke it. Some of the reasons being:
The following example will check to see if the encode action of Barcode Scanner exists and load it with dummy data ("123456789"). If the intent is unavailable, it will open up Barcode Scanner's page in the Android Market so the user can download it. In a real deployment you might want to add a messagebox informing the user of what is going on and possibly allowing him to cancel.
The following snippet will try to find if an activity exists so it can be called.
The following example, will check if the "Field Test" activity exists. Field Test is an application which gives low-level information about a phone's communication systems and it only exists on HTC devices running Sense. If the Activity exists, it will create the respective menu item.
Acknowledgements: The first function is taken from http://android-developers.blogspot.com/2009/01/can-i-use-this-intent.html
For example, to scan a barcode using the Barcode Scanner application, or to start the "hidden" Field Test application in HTC phones.
In order to make the life of a user easier, it is a good idea to check if the Intent Receiver/Activity exists before you attempt to invoke it. Some of the reasons being:
- If a non-existent intent, you application will force close.
- If the intent receiver is not present, you might want to redirect the user to the market to download the necessary application.
- Depending on the existence of an intent you might want to make menu options appear/disappear.
public boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(action);
List resolveInfo =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo.size() > 0) {
return true;
}
return false;
}
The following example will check to see if the encode action of Barcode Scanner exists and load it with dummy data ("123456789"). If the intent is unavailable, it will open up Barcode Scanner's page in the Android Market so the user can download it. In a real deployment you might want to add a messagebox informing the user of what is going on and possibly allowing him to cancel.
if (isIntentAvailable(this, "com.google.zxing.client.android.ENCODE")){
Intent i = new Intent();
i.setAction("com.google.zxing.client.android.ENCODE");
i.putExtra ("ENCODE_TYPE", "TEXT_TYPE");
i.putExtra ("ENCODE_DATA", "123456789");
startActivity(i);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(market://details?id=com.google.zxing.client.android));
startActivity(intent);
}
The following snippet will try to find if an activity exists so it can be called.
public boolean isActivityAvailable(Context context, String packageName, String className) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent();
intent.setClassName(packageName, className);
List list = packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
The following example, will check if the "Field Test" activity exists. Field Test is an application which gives low-level information about a phone's communication systems and it only exists on HTC devices running Sense. If the Activity exists, it will create the respective menu item.
/** Creates the menu items */
public boolean onCreateOptionsMenu(Menu menu) {
Boolean fieldTest = isActivityAvailable("com.htc.fieldtest", "com.htc.fieldtest.FieldTestActivity");
menu.add(0, MENU_BUTTONS.REFRESH.ordinal(), 0,
getString(R.string.label_menu_refresh)).setIcon(android.R.drawable.ic_menu_rotate);
menu.add(0, MENU_BUTTONS.EXPORT.ordinal(), 0,
getString(R.string.label_menu_export)).setIcon(android.R.drawable.ic_menu_upload);
menu.add(0, MENU_BUTTONS.ABOUT.ordinal(), 0,
getString(R.string.label_menu_about)).setIcon(android.R.drawable.ic_menu_info_details);
if (fieldTest){
menu.add(0, MENU_BUTTONS.FIELD_TEST.ordinal(), 0,
getString(R.string.label_menu_field_test)).setIcon(android.R.drawable.ic_menu_manage);
}
return true;
}
Acknowledgements: The first function is taken from http://android-developers.blogspot.com/2009/01/can-i-use-this-intent.html
The second part of the example won't work unless you include a context in the function call. FYI.
ReplyDeleteAny idea how to unit test this? I've been searching for a way to shadow the PackageManager but found nothing.
ReplyDeleteΚαλησπέρα! Θα μπορούσατε να μου πείτε πώς λέγεται το "intent handler" στα Ελληνικά; Ευχαριστώ πολύ! :)
ReplyDeleteGood sharing! I need a javascript encode number interleafed barcode, but I haven't any idea, can you help me? Thanks a lot.
ReplyDeleteHow to scan a barcode using Barcode Scanner application
ReplyDeletehttps://bayanlarsitesi.com/
ReplyDeleteCihangir
Heybeliada
Yalova
Adana
VG23
Eskişehir
ReplyDeleteDenizli
Malatya
Diyarbakır
Kocaeli
30F7D
A5A7D
ReplyDeleteErzincan Evden Eve Nakliyat
Artvin Lojistik
Düzce Parça Eşya Taşıma
Erzurum Parça Eşya Taşıma
Aksaray Evden Eve Nakliyat
72735
ReplyDeleteEskişehir Şehirler Arası Nakliyat
Elazığ Lojistik
Tokat Parça Eşya Taşıma
Maraş Lojistik
Bitmart Güvenilir mi
Manisa Lojistik
Afyon Şehir İçi Nakliyat
Trabzon Şehir İçi Nakliyat
Çerkezköy Kombi Servisi
DBFA1
ReplyDeleteÇerkezköy Petek Temizleme
Ağrı Şehir İçi Nakliyat
Zonguldak Evden Eve Nakliyat
Erzurum Lojistik
Muş Şehir İçi Nakliyat
Osmaniye Lojistik
Yenimahalle Boya Ustası
Yalova Evden Eve Nakliyat
Afyon Lojistik
61E2E
ReplyDeleteKripto Para Çıkarma Siteleri
Coin Üretme Siteleri
Bitcoin Kazanma
Görüntülü Sohbet
Linkedin Takipçi Hilesi
Jns Coin Hangi Borsada
Gate io Borsası Güvenilir mi
Raca Coin Hangi Borsada
Telegram Görüntüleme Satın Al
E74B1
ReplyDeletedcent wallet web
ledger live wallet web
ledger
ledger web
wallet arculus
wallet bitbox
web trust wallet
desktop ledger live
eigenlayer web