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

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




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值