首页 > 移动平台 > 详细

android.content.ActivityNotFoundException: No Activity found to handle Intent

时间:2014-08-27 16:03:28      阅读:553      评论:0      收藏:0      [点我收藏+]

 

代码如下:

    public void sendMessage(String number) {
        if (TextUtils.isEmpty(number)) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_SENDTO,
                Uri.fromParts(Constants.SCHEME_SMS, number, null));
        context.startActivity(intent);
    }

异常信息提示如下:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.SENDTO dat=sms:xxxxxxxxxxx } 

    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1632)

    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)

    at android.app.Activity.startActivityForResult(Activity.java:3424)

调查如下:

1,如果手机中没有能发送短信的app就会报出这样的错

2,手机中的能发送短信的应用被关闭了(设置-->应用-->app-->关闭);

解决方法:为了避免有的手机没有打开相应文件的app,在startActivity那里做一个try catch

    public void sendMessage(String number) {
        if (TextUtils.isEmpty(number)) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_SENDTO,
                Uri.fromParts(Constants.SCHEME_SMS, number, null));
        try {
            context.startActivity(intent);
        } catch(ActivityNotFoundException exception) {
            Toast.makeText(this, "no activity", Toast.LENGTH_SHORT).show();
        }
    }

or 调用系统方法判断是否有对应的app

     public void sendMessage(String number) {
        if (TextUtils.isEmpty(number)) {
            return;
        }
        Intent intent = new Intent(Intent.ACTION_SENDTO,
                Uri.fromParts(Constants.SCHEME_SMS, number, null));
        
        PackageManager packageManager = getPackageManager();
        List<ResolveInfo>applist = packageManager.queryIntentActivities(intent, 0);
        if (applist == null || applist.isEmpty()) {
            Toast.makeText(this, "no activity", Toast.LENGTH_SHORT).show();
            return;
        }
        context.startActivity(intent);
    }

 

android.content.ActivityNotFoundException: No Activity found to handle Intent

原文:http://www.cnblogs.com/xueqiang911226/p/3939374.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!