0

我有以下代码可以从远程查看器中提取 pdf 文档以进行查看:

package com.example.techvault;

import java.io.File;
import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

public class PDFFromServerActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_document);
        Intent in= getIntent(); // gets the previously created intent
        String url = in.getStringExtra("full_url");
        if (!url.startsWith("http://") && !url.startsWith("https://")){
            url = "http://ezdrawdocs.com" + url;
        }
        Log.d("pdf url: ",url);
        String extStorageDirectory = Environment.getExternalStorageDirectory()
        .toString();
        File folder = new File(extStorageDirectory, "pdf");
        folder.mkdir();
        File file = new File(folder, "Read.pdf");
        try {
            file.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        Downloader.DownloadFile(url, file);

        showPdf();
    }
    public void showPdf()
        {
            File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
            PackageManager packageManager = getPackageManager();
            Intent testIntent = new Intent(Intent.ACTION_VIEW);
            testIntent.setType("application/pdf");
            List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            Uri uri = Uri.fromFile(file);
            intent.setDataAndType(uri, "application/pdf");
            startActivity(intent);
        }
}

选择后退箭头时,它会重定向到空白屏幕。选择后退箭头时,如何控制要重定向到的活动?谢谢

4

2 回答 2

0

您在这里所做的是实现我所说的“直通活动”(没有 UI 并立即启动不同活动的活动)......我个人尽量避免它们。

也就是说,您需要做的是在inActivity.finish()之后调用。这将关闭您的直通,这样当您退出 PDF 查看器时,您将不会返回直通。showPDF()Activity.onCreate()

于 2012-12-17T17:05:00.153 回答
0

您可以在活动下的清单中定义它

<meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value="com.example.yourapp.SomeActivity" />
于 2012-12-17T14:35:42.360 回答