Android : Can I use this intent from a 3rd party application?
I'm using an intent to post a message via a Twitter client. When there is no Twitter application on the phone I want to redirect the user to the market. But the exception ActivityNotFoundException is not working. Everytime (when I don't have a Twitter app) I get the error "No applications can perform this action"
Intent intentTwitter = new Intent(Intent.ACTION_SEND);
intentTwitter.putExtra(Intent.EXTRA_TEXT,msg);
intentTwitter.setType("application/twitter");
try{
startActivity(Intent.createChooser(intentTwitter,"tweet"));
}catch(ActivityNotFoundException e){
// lead to the app market
}
I read ActivityNotFoundException is the exception handler for startActivity and its child. Maybe the solution is not in the exception handling.
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.
public static boolean isIntentAvailable(Context context, String action) {
final PackageManager packageManager = context.getPackageManager();
final Intent intent = new Intent(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
public void ConnectTwitter(){
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() {
public void onClick(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.
public void intentMarket (String url){
Intent i = new Intent(Intent.ACTION_VIEW);
Uri u = 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 !
我建议使用PackageManager
和queryIntentActivities()
来确定是否有某件事会处理你的startActivity()
请求。