安卓:我可以用这个意图从第三方应用程序? [英] Android : Can I use this intent from a 3rd party application?

查看:187
本文介绍了安卓:我可以用这个意图从第三方应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是意图通过微博客户端发布的消息。
当在手机上没有Twitter应用我想将用户重定向到市场。
但例外ActivityNotFoundException不工作。每次(当我没有Twitter的应用程序)我得到没有应用程序可执行此操作错误

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
}

我看了ActivityNotFoundException是startActivity及其子异常处理程序。也许该解决方案是不是在异常处理。

I read ActivityNotFoundException is the exception handler for startActivity and its child. Maybe the solution is not in the exception handling.

推荐答案

下面是解决方案发布。

我使用软件包管理系统和queryIntentActivities()来指示指定的动作是否可以用作意图。
该方法查询包管理器,可以与指定的操作意图做出回应在手机上安装的软件包。如果没有找到包,则该方法返回false。

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;
    }

下面是完整的code。我连接到Twitter与Twitter客户端。所以我用

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();     
    }

}

与intentMarket method.Just输入URL =市场://搜Q = Twitter的?
顺便说一句,市场没有安装在模拟器

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();  
    }
}

更多关于软件包管理系统
http://android-developers.blogspot.com /2009/01/can-i-use-this-intent.html

竖起大拇指,如果你发现这很有用!

Thumbs up if you find this useful !

这篇关于安卓:我可以用这个意图从第三方应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆