Android : Can I Use This Intent From A 3rd Party Application?
Solution 1:
Here is the solution posted.
I use PackageManager and queryIntentActivities() to indicate whether the specified action can be used as an intent. The method queries the package manager for installed packages on the phone that can respond to an intent with the specified action. If no packages is found , the method returns false.
publicstaticbooleanisIntentAvailable(Context context, String action) {
finalPackageManagerpackageManager= context.getPackageManager();
finalIntentintent=newIntent(action);
List<ResolveInfo> list =
packageManager.queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
Here is the complete code. I connect to Twitter with a Twitter client. So I'm using
publicvoidConnectTwitter(){
String msg = getResources().getString(R.string.partager_twitter).toString();
Intent intentTwitter = new Intent(Intent.ACTION_SEND);
intentTwitter.putExtra(Intent.EXTRA_TEXT,msg);
intentTwitter.setType("application/twitter");
if (isIntentAvailable(this,"application/twitter")){
startActivity(Intent.createChooser(intentTwitter,getResources().getString(R.string.partager_sel_tweet)));
}
else{
/* Handle Exception if no suitable apps installed */
Log.d("twitter", "Catch exception");
new AlertDialog.Builder(PartagerActivity.this)
.setTitle(getResources().getString(R.string.partager_sel_tweet))
.setMessage(getResources().getString(R.string.partager_app_download))
.setNegativeButton("Non", null)
.setPositiveButton("Oui", new DialogInterface.OnClickListener() {
publicvoidonClick(DialogInterface dialog, int whichButton) {
intentMarket("market://search?q=twitter");
}
})
.show();
}
}
with intentMarket method.Just enter the url ="market://search?q=twitter" BTW the market is not installed in the emulator.
publicvoidintentMarket(String url){
Intenti=newIntent(Intent.ACTION_VIEW);
Uriu= Uri.parse(url);
i.setData(u);
try{
startActivity(i);
}
catch(ActivityNotFoundException e){
Toast.makeText(this, "Pas d'applications twitter trouvé.", Toast.LENGTH_SHORT).show();
}
}
More about PackageManager http://android-developers.blogspot.com/2009/01/can-i-use-this-intent.html
Thumbs up if you find this useful !
Solution 2:
I recommend using PackageManager and queryIntentActivities() to determine if there is something that will handle your startActivity() request.
Post a Comment for "Android : Can I Use This Intent From A 3rd Party Application?"