Android隐式意图验证

时间:2015-12-08 10:29:34

标签: android android-intent

在我的应用程序中,我将数据保存到Excel文件中,并为用户提供:

  1. 在设备上打开已保存的Excel文件;
  2. 在文件管理器(包含文件夹)中显示文件。
  3. 发送通过电子邮件附加的文件(实际打开电子邮件客户端并附加文件)。
  4. 所有东西都可以在我的设备上正常工作,但问题是:

    • 如何验证这些隐含意图?
    • 假设没有可用的Excel查看器/文件管理器/电子邮件客户端?
    • 我该如何预测呢?
    • 我应该添加任何其他验证吗?

    CODE

        /**
         * Opening saved file.
         * 
         * @param file
         *            - File to be opened
         */
    
        private void openSavedFile(File file) {
            Uri uri = Uri.fromFile(file);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(uri);
            this.startActivity(intent);
        }
    
        /**
         * Open a file browser and shows the folder which contains the file passed
         * as a parameter.
         * 
         * @param file
         *            - File to be shown in file browser
         */
        public void openFolder(File file) {
            if (file.exists()) {
                file = file.getParentFile();
    
                Uri selectedUri = Uri.fromFile(file);
    
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(selectedUri, "resource/folder");
                startActivity(intent);
            }
        }
    
        /**
         * Sends a file by email in attachment
         * 
         * @param file
         *            - file to be sent
         */
        private void sendReportByMail(File file) {
    
            Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
            // setting the type
            emailIntent.setData(Uri.parse("mailto:"));
    
            // the attaching the file
            emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    
            // the mail subject
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, createEmailSubject());
    
            // starting email intent with chooser
            startActivity(Intent.createChooser(emailIntent, this.getResources()
                    .getString(R.string.report_email_chooser)));
    
        }
    

1 个答案:

答案 0 :(得分:3)

检查你触发的隐含意图,任何活动都是他们处理那个火灾意图

PackageManager packageManager = getActivity().getPackageManager();
    if (intent.resolveActivity(packageManager) != null) {
        //Gotactivity to handle intent
        startActivity(intent);
    }   else  {
        Log.d(TAG, "No Intent available to handle action");
    }