java - Android - 为什么我的 Toast 消息不显示?

标签 java android toast

我的 toast 没有出现,我很困惑,因为我认为我做得正确,但我不明白。正如您所看到的,它是一个显示三个按钮的程序,第一个按钮会激活下载,该下载应该会自动打开 PDF 文件 - 如果没有 pdf 阅读器,则会弹出一个提示,提示没有阅读器。其他按钮将进入新屏幕并用作后退按钮。

public class myactivity extends Activity {

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private Button startBtn;
private ProgressDialog mProgressDialog;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myactivity);
    mProgressDialog = new ProgressDialog(myactivity.this);
    mProgressDialog.setMessage("Please be patient, file downloading...");
    mProgressDialog.setIndeterminate(false);
    mProgressDialog.setMax(100);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

    Button Section = (Button) findViewById(R.id.Section);
    Section.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            // TODO Auto-generated method stub
            Intent Section = new Intent(view.getContext(),
                    Section.class);
            startActivity(Section);

        }
    });

    Button Back = (Button) findViewById(R.id.Back);
    Back.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            setResult(RESULT_OK);
            finish();
        }
    });

    startBtn = (Button) findViewById(R.id.Search);
    startBtn.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            startDownload();
        }
    });
}

private void startDownload() {
    DownloadFile downloadFile = new DownloadFile();
    downloadFile
            .execute("http://www.website.com/document.pdf");

}

class DownloadFile extends AsyncTask<String, Integer, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        mProgressDialog.show();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        super.onProgressUpdate(progress);
        mProgressDialog.setProgress(progress[0]);
    }

    @Override
    protected String doInBackground(String... aurl) {
        try {

            URL url = new URL(aurl[0]);
            URLConnection connection = url.openConnection();

            connection.connect();
            int fileLength = connection.getContentLength();
            int tickSize = 2 * fileLength / 100;
            int nextProgress = tickSize;

            Log.d(

            "ANDRO_ASYNC", "Lenght of file: " + fileLength);

            InputStream input = new BufferedInputStream(url.openStream());

            String path = Environment.getExternalStorageDirectory()
                    + "/Android/Data/"
                    + getApplicationContext().getPackageName() + "/files";
            File file = new File(path);
            file.mkdirs();
            File outputFile = new File(file, "test1.pdf");

            OutputStream output = new FileOutputStream(outputFile);

            byte data[] = new byte[1024 * 1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                if (total >= nextProgress) {
                    nextProgress = (int) ((total / tickSize + 1) * tickSize);
                    this.publishProgress((int) (total * 100 / fileLength));
                }
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
            showPdf();

        } catch (Exception e) {
        }
        return null;
    }

}

private void showPdf() {
    // TODO Auto-generated method stub
    mProgressDialog.dismiss();

    File file = new File(Environment.getExternalStorageDirectory()
            + "/Android/Data/" + getApplicationContext().getPackageName()
            + "/files/test1.pdf");
    Intent testIntent = new Intent(Intent.ACTION_VIEW);
    testIntent.setType("application/pdf");
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "application/pdf");
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(myactivity .this,
                "No Application Available to View PDF", Toast.LENGTH_LONG).show();
    }

}

}

最佳答案

 try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(myactivity .this,
                "No Application Available to View PDF", Toast.LENGTH_LONG).show();
    }

上述代码仅在您未在 list 文件中声明 Activity 时显示 toast,但在 pdf 不存在时则不会显示。您必须使用 FileNotFoundException 来满足您的要求。

已更新:::

try{
 File file = new File(Environment.getExternalStorageDirectory()
            + "/Android/Data/" + getApplicationContext().getPackageName()
            + "/files/test1.pdf");
    Intent testIntent = new Intent(Intent.ACTION_VIEW);
    testIntent.setType("application/pdf");
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "application/pdf");
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        Toast.makeText(myactivity .this,
                "No Application Available to View PDF", Toast.LENGTH_LONG).show();
    }
} catch (FileNotFoundException e) {
        Toast.makeText(myactivity .this,
                "No Application Available to View PDF", Toast.LENGTH_LONG).show();
    }

关于java - Android - 为什么我的 Toast 消息不显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10458512/

相关文章:

Android 主屏幕小部件未更新

Android BLE Beacon 扫描

android - 如何在显示时动态更改 Toast 通知中的文本?

javascript - 如何在带有 react-alert 的组件中显示消息?

java - 访问者模式与开放/封闭原则 : how to add new visitable object?

java - 部署在不同供应商的不同应用程序服务器中的 EJB 之间的通信

java - 从标记创建语法树

java - OpenSuse 13.2 上的 Qt for Android

android - 捕获 toast 事件(来自任何应用程序)并获取 toast 消息

java - 如何使示例 MulticastChat 运行?