2016-11-20 151 views
0

我遇到以下問題。未安裝pdf閱讀器時未顯示消息

當一個文件被下載時,我會向用戶顯示一個通知,當他/她選擇通知時,它會搜索適用的應用程序(例如PDF閱讀器)以打開文件。

Eveything在我選中通知但是沒有安裝PDF閱讀器時工作沒有烤麪包信息顯示給用戶。

有人可以幫助我嗎?我知道try塊是空的,因爲我不知道到底是什麼來調用toast消息。

謝謝。

編輯:它的工作原理當我取消註釋「context.startActivity(目標);」但是這會自動啓動打開的過程,它應該在用戶選擇通知時啓動。

通知碼:

if (file.getName().endsWith(".pdf")) { 
      Intent install = openPdf(urlPath, context, mNotificationManager, 
        NOTIFYCATIONID); 
      PendingIntent pending = PendingIntent.getActivity(context, 0, install, 0); 

      mBuilder = new NotificationCompat.Builder(context) 
        .setContentTitle(appName) 
        .setContentText("ready to open pdf."); 
      mBuilder.setContentIntent(pending); 
      mBuilder.setSmallIcon(R.drawable.placeholder); 
      mBuilder.setDefaults(Notification.DEFAULT_SOUND); 
      mBuilder.setAutoCancel(true); 
      mNotificationManager = 
        (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build()); 
     } 

代碼以打開PDF文件:

public static Intent openPdf(String urlPath, Context context, 
            NotificationManager mNotificationManager, int NOTIFYCATIONID) { 
     File file = new File(urlPath); 
     MimeTypeMap mime = MimeTypeMap.getSingleton(); 
     String ext = file.getName().substring(file.getName().lastIndexOf(".")+1); 
     String type = mime.getMimeTypeFromExtension(ext).toLowerCase();; 

     Intent target = new Intent(Intent.ACTION_VIEW, Uri.fromFile(file)); 
     target.setDataAndType(Uri.fromFile(file), type); 
     target.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); 

     try { 
      //context.startActivity(target); 
     } catch (ActivityNotFoundException e) { 
      Toast.makeText(context, "No application found to open PDF, please install one.", Toast.LENGTH_SHORT).show(); 
     } 

     mNotificationManager.cancel(NOTIFYCATIONID); 
     return target; 
    } 

回答

2

你不應該試圖啓動一個活動,以確定它是否存在。相反,您可以使用PackageManager檢查是否存在可以處理您的意圖的Activity,而無需啓動它。

嘗試(他們都是相當)以下的方法之一:

PackageManager pm = context.getPackageManager(); 
if (pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) == null) { 
    Toast.makeText(context, "No application found to open PDF, please install one.", Toast.LENGTH_SHORT).show(); 
} 

或:

PackageManager pm = context.getPackageManager(); 
if (intent.resolveActivity(pm) == null) { 
    Toast.makeText(context, "No application found to open PDF, please install one.", Toast.LENGTH_SHORT).show(); 
} 

或:

PackageManager pm = context.getPackageManager(); 
if (pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY) == 0) { 
    Toast.makeText(context, "No application found to open PDF, please install one.", Toast.LENGTH_SHORT).show(); 
} 

,取而代之的取消在openPdf通知如果沒有可用的應用,則只需返回null不要試圖顯示通知。

+0

Thx Marcin,我得到一個錯誤,說:resolveActivity(PackageManager)不能應用於(PackageManager,int)。 我把你的代碼放在openPDF方法中。 (target.resolveActivity(pm,PackageManager.MATCH_DEFAULT_ONLY)== null) – Simon

+0

是的,對不起。該方法應該在下午不打算的時候被調用。我更新了我的答案。 –

+0

由於類似問題的答案中提到了幾種等效的方法,我只是將它們全部列出。 –