Java 安卓-为什么赢了';难道我的祝酒词没有显示出来吗?
我的祝酒词没有出现,我很困惑,因为我认为我做对了,我不明白。正如你所看到的,这是一个显示三个按钮的程序,第一个按钮激活一个下载,该下载应该自动打开pdf文件-如果没有pdf阅读器,应该弹出一个祝酒词,说没有阅读器。其他按钮进入新屏幕并作为后退按钮操作Java 安卓-为什么赢了';难道我的祝酒词没有显示出来吗?,java,android,toast,Java,Android,Toast,我的祝酒词没有出现,我很困惑,因为我认为我做对了,我不明白。正如你所看到的,这是一个显示三个按钮的程序,第一个按钮激活一个下载,该下载应该自动打开pdf文件-如果没有pdf阅读器,应该弹出一个祝酒词,说没有阅读器。其他按钮进入新屏幕并作为后退按钮操作 public class myactivity extends Activity { public static final int DIALOG_DOWNLOAD_PROGRESS = 0; private Button startBtn; p
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();
}
}
}
公共类myactivity扩展活动{
公共静态最终整型对话框\u下载\u进度=0;
专用按钮startBtn;
private ProgressDialog mProgressDialog;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
mProgressDialog=newprogressdialog(myactivity.this);
设置消息(“请耐心等待,文件下载…”);
mProgressDialog.setUndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_水平);
按钮部分=(按钮)findViewById(R.id.Section);
Section.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图){
//TODO自动生成的方法存根
Intent节=新的Intent(view.getContext(),
第三节(课程);
星触觉(部分);
}
});
按钮返回=(按钮)findViewById(R.id.Back);
Back.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//TODO自动生成的方法存根
设置结果(结果\正常);
完成();
}
});
startBtn=(按钮)findViewById(R.id.Search);
setOnClickListener(新的OnClickListener(){
公共void onClick(视图v){
startDownload();
}
});
}
私有void startDownload(){
DownloadFile DownloadFile=新的DownloadFile();
下载文件
.执行(”http://www.website.com/document.pdf");
}
类DownloadFile扩展异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
mProgressDialog.show();
}
@凌驾
受保护的void onProgressUpdate(整数…进度){
super.onProgressUpdate(进度);
mProgressDialog.setProgress(progress[0]);
}
@凌驾
受保护的字符串背景(字符串…aurl){
试一试{
URL=新URL(aurl[0]);
URLConnection=url.openConnection();
connection.connect();
int fileLength=connection.getContentLength();
int tickSize=2*fileLength/100;
int-nextProgress=tickSize;
对数d(
“ANDRO_ASYNC”,“文件长度:”+fileLength);
InputStream输入=新的BufferedInputStream(url.openStream());
String path=Environment.getExternalStorageDirectory()
+“/Android/Data/”
+getApplicationContext().getPackageName()+“/files”;
文件=新文件(路径);
mkdirs()文件;
File outputFile=新文件(文件“test1.pdf”);
OutputStream output=新文件OutputStream(outputFile);
字节数据[]=新字节[1024*1024];
长总计=0;
整数计数;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
如果(总计>=下一步){
nextProgress=(int)((总计/tickSize+1)*tickSize);
此.publishProgress((int)(总计*100/文件长度));
}
输出.写入(数据,0,计数);
}
output.flush();
output.close();
input.close();
showPdf();
}捕获(例外e){
}
返回null;
}
}
私人文件(PDF){
//TODO自动生成的方法存根
mProgressDialog.disclose();
File File=新文件(Environment.getExternalStorageDirectory()
+“/Android/Data/”+getApplicationContext().getPackageName()
+“/files/test1.pdf”);
Intent testIntent=新的意图(Intent.ACTION\u视图);
testIntent.setType(“application/pdf”);
意图=新意图();
intent.setAction(intent.ACTION\u视图);
Uri=Uri.fromFile(文件);
setDataAndType(uri,“应用程序/pdf”);
试一试{
星触觉(意向);
}捕获(ActivityNotFounde异常){
Toast.makeText(myactivity.this,
“没有可用于查看PDF的应用程序”,Toast.LENGTH_LONG.show();
}
}
}
以上代码仅在您尚未在Manifeat文件中声明活动时显示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();
}
我应该始终将文件放在那里,因此,如果我更改为FileNotFound,则无论是否安装了正确的pdf读取器,它都只会看到文件,而不会显示toast?尝试将“ActivityNotFoundException”替换为“FileNotFoundException”,会出现以下错误:“FileNotFoundException”的捕获块不可访问。从不从try语句体引发此异常“感谢更新,但在FileNotFoundException的行上,eclipse仍在引发错误。错误状态为“FileNotFoundException的不可访问捕获块”。这个异常永远不会从try语句体中抛出“它的选项是删除catch子句吗?相反,try catch(exception e){}捕获所有类型的异常(exception e)eclipse的代码很好,但什么都没有发生。我没有收到Toast消息。困惑。。。
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();
}