2015-10-05 91 views
0

我正在開發一個android應用程序。它通過推送通知接收URL。我需要用chrome打開網址。我試圖用這個,但不工作Android open通知

Intent i; 
    try { 
     i = new Intent(Intent.ACTION_VIEW); 
     i.setType("text/html"); 
     i.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); 
     i.setComponent(ComponentName.unflattenFromString("com.android.chrome/com.android.chrome.Main")); 
     i.addCategory("android.intent.category.LAUNCHER"); 
     i.setData(Uri.parse(audioLink)); 
    } catch (ActivityNotFoundException ex) { 
     i = new Intent(Intent.ACTION_VIEW, Uri.parse(audioLink)); 
    } 

    PendingIntent pendingIntent = 
      PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_ONE_SHOT); 

    NotificationCompat.Builder builder; 
    NotificationManager manager; 

    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    builder = new NotificationCompat.Builder(this) 
      .setContentTitle(getString(R.string.newmessagetitle)) 
      .setContentText(getString(R.string.newmessage)) 
      .setSmallIcon(R.mipmap.ic_stat_lollipop_icon); 

    builder.setContentIntent(pendingIntent); 

    int defaults = 0; 
    defaults = defaults | Notification.DEFAULT_LIGHTS; 
    defaults = defaults | Notification.DEFAULT_VIBRATE; 
    defaults = defaults | Notification.DEFAULT_SOUND; 

    builder.setDefaults(defaults); 
    builder.setAutoCancel(true); 

    manager.notify(notifyID, builder.build()); 
+0

你可以在第一種情況下使用ACTION_VIEW而不是ACTION_MAIN,並指定MIME_TYPE? – geokavel

+0

指定MIME_TYPE的含義是什麼? –

+0

i.setType(type),並使用表示數據類型的MIME_TYPE,例如:https://developer.android.com/intl/zh-tw/guide/components/intents-common。 html#瀏覽器 – geokavel

回答

0

我真的認爲這樣的事情應該爲意圖代碼工作。如果沒有,有可能出錯代碼的另一部分:

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(audioLink)); 
i.setPackage("com.android.chrome"); 

由ActivityNotFoundException是在那個地方完全無用的方式,因爲只有Activity.startActivity()拋出該異常。如果你想檢查是否安裝了Chrome,我會使用:How can I learn whether a particular package exists on my Android device?