如何在 Android 網頁檢視中顯示 pdf 文件?


此示例說明如何以程式設計方式鎖定 Android 裝置。

步驟 1 - 在 Android Studio 中新建一個專案,轉到檔案 ⇒ 新建專案並填寫所有必需的詳細資訊來新建一個專案。

步驟 2 - 將以下程式碼新增到 res/layout/activity_main.xml

<? xml version= "1.0" encoding= "utf-8" ?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
   xmlns: tools = "http://schemas.android.com/tools"
   android :layout_width= "match_parent"
   android :layout_height= "match_parent"
   android :layout_margin= "16dp"
   tools :context= ".MainActivity" >
   <Button
      android :onClick= "loadPage"
      android :layout_width= "match_parent"
      android :layout_height= "wrap_content"
      android :text= "Load web Page" />
</RelativeLayout>

步驟 3 - 將以下程式碼新增到 src/MainActivity

package app.tutorialspoint.com.sample ;
import android.os.Bundle ;
import android.support.v7.app.AppCompatActivity ;
import android.view.View ;
import android.webkit.WebView ;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate (Bundle savedInstanceState) {
      super .onCreate(savedInstanceState) ;
      setContentView(R.layout. activity_main ) ;
   }
   public void loadPage (View view) {
      WebView webview = new WebView( this ) ;
      webview.getSettings().setJavaScriptEnabled( true ) ;
      setContentView(webview) ;
      String pdf =
         "http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf" ;
      webview.loadUrl( "http://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf) ;
   }
}

步驟 4 - 將以下程式碼新增到 androidManifest.xml

<? xml version= "1.0" encoding= "utf-8" ?>
<manifest xmlns: android = "http://schemas.android.com/apk/res/android"
   package= "app.tutorialspoint.com.sample" >
   <application
      android :allowBackup= "true"
      android :icon= "@mipmap/ic_launcher"
      android :label= "@string/app_name"
      android :roundIcon= "@mipmap/ic_launcher_round"
      android :supportsRtl= "true"
      android :theme= "@style/AppTheme" >
      <activity android :name= ".MainActivity" >
         <intent-filter>
            <action android :name= "android.intent.action.MAIN" />
            <category android :name= "android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
      <receiver
         android :name= ".DeviceAdmin"
         android :description= "@string/app_description"
         android :label= "@string/app_name"
         android :permission= "android.permission.BIND_DEVICE_ADMIN" >
         <meta-data
            android :name= "android.app.device_admin"
            android :resource= "@xml/policies" />
         <intent-filter>
            <action android :name= "android.app.action.DEVICE_ADMIN_ENABLED" />
         </intent-filter>
      </receiver>
   </application>
</manifest>

讓我們嘗試執行你的應用程式。我假設你已將實際的 Android 移動裝置連線到計算機。要從 Android Studio 執行該應用,請開啟一個專案的活動檔案並從工具欄單擊 執行 圖示。選擇移動裝置作為選項,然後檢查移動裝置,它將顯示你的預設 。

更新於: 2019 年 7 月 30 日

2 千次觀看

開啟您的職業生涯之旅

透過完成課程取得認證

開始使用
廣告