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

标签 android twitter exception-handling android-intent

我正在使用 intent 通过 Twitter 客户端发布消息。 当手机上没有 Twitter 应用程序时,我想将用户重定向到市场。 但是异常 ActivityNotFoundException 不起作用。每次(当我没有 Twitter 应用程序时)我都会收到错误消息“没有应用程序可以执行此操作”

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 及其子项的异常处理程序。也许解决方案不在异常处理中。

最佳答案

这是发布的解决方案。

我使用 PackageManager 和 queryIntentActivities() 来指示指定的操作是否可以用作 Intent 。 该方法向包管理器查询手机上已安装的包,这些包可以通过指定的操作响应 Intent 。如果没有找到包,该方法返回 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;
    }

这是完整的代码。我使用 Twitter 客户端连接到 Twitter。所以我正在使用

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 方法。只需输入 url ="market://search?q=twitter" 顺便说一句,市场没有安装在模拟器中。

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

觉得有用就点个赞吧!

关于安卓 : Can I use this intent from a 3rd party application?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4336088/

相关文章:

php - Symfony2 异常的自定义错误模板

c# - 访问冲突 : Attempted to read or write protected memory

javascript - Greasemonkey 脚本只能通过 CTRL + F5 加载?

python - 使用计算成本低廉的 Python 哈希算法检测转推

api - 通过 API 创建 gmail、facebook 和 twitter 帐户?

c# - Windows 窗体应用程序中异常处理的最佳实践?

java - 使用多个微调器过滤 ListView ?

java - 使用袖珍狮身人面像进行语音识别的准确性很差

Android - 'sensorPortrait' 方向不工作

android - float 操作按钮(fab)行为在隐藏后停止 onNestedScroll