2012-02-28 8 views
6

今、私はアプリケーションを作成しています。私のアプリのユーザーは、pdfファイルを読むことができます。もしpdfリーダーがなければ、私のアプリは自動的にサイトからそれをインストールします。 これは私がpdfファイルを読むために使用したコードです。プログラムでadobe readerをダウンロードする方法はありません

File file = new File("/sdcard/sample.pdf"); 
PackageManager packageManager = getPackageManager(); 
Intent testIntent = new Intent(Intent.ACTION_VIEW); 
testIntent.setType("application/pdf"); 
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY); 
if (list.size() > 0 && file.isFile()) { 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_VIEW); 
    Uri uri = Uri.fromFile(file); 
    intent.setDataAndType(uri, "application/pdf"); 
    startActivity(intent); 
} 

私の疑問は、次のとおりです。

  1. 電話またはインストールされていませんアドビリーダーがあるどうかを確認する方法?
  2. プログラムで携帯電話にAdobe Readerをインストールするには?あなたのコードいくつかの合併症..

    使用から

答えて

19

このコード、

Intent intent; 
     intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(file, "application/pdf"); 
     try { 
      startActivity(intent); 
     } catch (ActivityNotFoundException e) { 
      // No application to view, ask to download one 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setTitle("No Application Found"); 
      builder.setMessage("Download one from Android Market?"); 
      builder.setPositiveButton("Yes, Please", 
        new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          Intent marketIntent = new Intent(Intent.ACTION_VIEW); 
          marketIntent 
            .setData(Uri 
              .parse("market://details?id=com.adobe.reader")); 
          startActivity(marketIntent); 
         } 
        }); 
      builder.setNegativeButton("No, Thanks", null); 
      builder.create().show(); 
     } 
    } 
+0

私はエミュレータでurコードをテストしました...そのalertdialogボックスが来ました。そして、私はokを押してから例外を与えます。何を考えていますか?私はエミュレータでテストしました。それは電話で動作しますか? – sarath

+0

これは私がこれまで行ってきた方法です – TerryProbert

+0

エミュレータは市場アプリケーションに付属していませんhttp://stackoverflow.com/questions/3994923/no-marketplace-application-on-the-android-emulator – TerryProbert

2

私は、これはあなたを助けるかもしれないと思う:

private void loadDocInReader(String doc) throws ActivityNotFoundException, Exception { 

try { 
    Intent intent = new Intent(); 

    intent.setPackage("com.adobe.reader"); 
    intent.setDataAndType(Uri.parse(doc), "application/pdf"); 

    startActivity(intent); 

} 

catch (ActivityNotFoundException activityNotFoundException) { 
      activityNotFoundException.printStackTrace(); 

      throw activityNotFoundException; 
} 
catch (Exception otherException) { 
      otherException.printStackTrace(); 

      throw otherException; 
} 
} 

アドビリーダーがあなたにインストールされていない場合ユーザーをこのURLにドラッグできます:

https://market.android.com/details?id=com.adobe.reader

これは、Android MarketモバイルアプリでAdobe Readerを開きます。ユーザーが望むならば、彼らはインストールすることができます。