Android : Can I use this intent from a 3rd party application?

笑着哭i 提交于 2019-12-12 12:20:35

问题


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.


回答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.

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 !




回答2:


I recommend using PackageManager and queryIntentActivities() to determine if there is something that will handle your startActivity() request.



来源:https://stackoverflow.com/questions/4336088/android-can-i-use-this-intent-from-a-3rd-party-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!