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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM