2017-05-07 6 views
1

Chrome에서 웹 사이트를 방문하여 <audio> 요소를 재생하면 Chrome에서 알림 영역에 재생/일시 중지 btn이 포함 된 알림을 추가합니다. webview 앱에서도 동일한 작업을 수행 할 수 있습니까?html로 된 WebView 앱 <audio> 요소 - 알림 영역에서 가능합니까?

나는 위로 안드로이드 스튜디오에서 실행되는 것을 얻을 관리했지만, 통지 일

모든 포인터를 알아낼 수 없습니다?

편집 : 다음은 의미하는 예입니다. https://developers.google.com/web/updates/2015/07/media-notifications 그게 가능합니까?

+0

저도 같은 문제에 대한 수집 한 무엇에 대한 나의 최선의 가정은 당신이해야한다는 것입니다 evaluateJavascript()를 사용하고 주입/가져 오기 – DrBrad

답변

0

여기에 제가 작성한 예가 있습니다. 여기에는 StackOverflow가 있지만, 이에 대한 문서는 없습니다.

https://gist.github.com/DrBrad/7574712c6140ccc468212afa04d9e458는 NotificationPausePlay.java

public class NotificationPausePlay extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent){ 
     Log.e("Here", "I am here"); 

     if(playing){ 
      if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ 
       wv.evaluateJavascript("videoElement.pause();", null); 
      }else{ 
       wv.loadUrl("javascript:videoElement.pause();"); 
      } 

      NotificationManager mNotificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification.Builder builder = new Notification.Builder(activity); 
      Notification notification = builder.getNotification(); 
      notification.icon = R.drawable.icon; 

      RemoteViews contentView = new RemoteViews(activity.getPackageName(), R.layout.custom_notification); 
      notification.contentView = contentView; 
      notification.flags |= Notification.FLAG_ONGOING_EVENT; 

      contentView.setTextViewText(R.id.title, "Browser"); 
      contentView.setTextViewText(R.id.desc, "This is a description. - PAUSED"); 
      contentView.setImageViewResource(R.id.pausePlay, R.drawable.play); 

      PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(activity, 0, new Intent(activity, NotificationPausePlay.class), 0); 
      contentView.setOnClickPendingIntent(R.id.pausePlay, pendingSwitchIntent); 

      mNotificationManager.notify(99, notification); 

      playing = false; 
     }else{ 
      if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ 
       wv.evaluateJavascript("videoElement.play();", null); 
      }else{ 
       wv.loadUrl("javascript:videoElement.play();"); 
      } 

      NotificationManager mNotificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification.Builder builder = new Notification.Builder(activity); 
      Notification notification = builder.getNotification(); 
      notification.icon = R.drawable.icon; 

      RemoteViews contentView = new RemoteViews(activity.getPackageName(), R.layout.custom_notification); 
      notification.contentView = contentView; 
      notification.flags |= Notification.FLAG_ONGOING_EVENT; 

      contentView.setTextViewText(R.id.title, "Browser"); 
      contentView.setTextViewText(R.id.desc, "This is a description. - PLAYING"); 
      contentView.setImageViewResource(R.id.pausePlay, R.drawable.play); 


      PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(activity, 0, new Intent(activity, NotificationPausePlay.class), 0); 
      contentView.setOnClickPendingIntent(R.id.pausePlay, pendingSwitchIntent); 

      mNotificationManager.notify(99, notification); 

      playing = true; 
     } 
    } 
} 

이를 넣어 JSInterface.java

public class JSInterface { 

    public static boolean playing; 

    @JavascriptInterface 
    public void videoAction(String type){ 
     Log.e("Info", type); 

     playing = Boolean.parseBoolean(type); 

     if(playing){ 
      NotificationManager mNotificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification.Builder builder = new Notification.Builder(activity); 
      Notification notification = builder.getNotification(); 
      notification.icon = R.drawable.icon; 

      RemoteViews contentView = new RemoteViews(activity.getPackageName(), R.layout.custom_notification); 
      notification.contentView = contentView; 
      notification.flags |= Notification.FLAG_ONGOING_EVENT; 

      contentView.setTextViewText(R.id.title, "Browser"); 
      contentView.setTextViewText(R.id.desc, "This is a description. - PLAYING"); 
      contentView.setImageViewResource(R.id.pausePlay, R.drawable.pause); 

      PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(activity, 0, new Intent(activity, NotificationPausePlay.class), 0); 
      contentView.setOnClickPendingIntent(R.id.pausePlay, pendingSwitchIntent); 

      mNotificationManager.notify(99, notification); 


     }else{ 
      NotificationManager mNotificationManager = (NotificationManager) activity.getSystemService(Context.NOTIFICATION_SERVICE); 
      Notification.Builder builder = new Notification.Builder(activity); 
      Notification notification = builder.getNotification(); 
      notification.icon = R.drawable.icon; 

      RemoteViews contentView = new RemoteViews(activity.getPackageName(), R.layout.custom_notification); 
      notification.contentView = contentView; 
      notification.flags |= Notification.FLAG_ONGOING_EVENT; 

      contentView.setTextViewText(R.id.title, "Browser"); 
      contentView.setTextViewText(R.id.desc, "This is a description. - PAUSED"); 
      contentView.setImageViewResource(R.id.pausePlay, R.drawable.play); 

      PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(activity, 0, new Intent(activity, NotificationPausePlay.class), 0); 
      contentView.setOnClickPendingIntent(R.id.pausePlay, pendingSwitchIntent); 

      mNotificationManager.notify(99, notification); 
     } 
    } 
} 

이를 넣어 WebChromeClient.java

@Override 
public void onProgressChanged(final WebView view, int newProgress){ 
    super.onProgressChanged(view, newProgress); 
    if(newProgress == 100){ 

     String code = "var videoElement;" + 
       "for(var i = 0; i < document.getElementsByTagName('video').length; i++){" + 
       " var vid = document.getElementsByTagName('video')[0];" + 
       " vid.onplay = function(){" + 
       "  videoElement = vid;" + 
       "  JSOUT.videoAction('true');" + 
       " };" + 
       " vid.onpause = function(){" + 
       "  videoElement = vid;" + 
       "  JSOUT.videoAction('false');" + 
       " };" + 
       "}"; 
     if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){ 
      wv.evaluateJavascript(code, null); 
     }else{ 
      wv.loadUrl("javascript:"+code); 
     } 
    } 
} 

이를 넣어이 넣어 custom_notification.xml

<?xml version="1.0" encoding="UTF-8"?> 
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="10dp" 
android:background="#e6e6e6"> 

<ImageView 
    android:layout_width="45dp" 
    android:layout_height="45dp" 
    android:id="@+id/icon" 
    android:src="@drawable/icon" 
    android:layout_centerVertical="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginRight="10dp" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/title" 
    android:textSize="16dp" 
    android:textStyle="bold" 
    android:textColor="#000000" 
    android:singleLine="true" 
    android:layout_alignParentTop="true" 
    android:layout_toRightOf="@+id/icon" 
    android:layout_toEndOf="@+id/icon" 
    android:layout_toLeftOf="@+id/pausePlay" 
    android:layout_toStartOf="@+id/pausePlay" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/desc" 
    android:textSize="16dp" 
    android:textColor="#5f5f5f" 
    android:singleLine="true" 
    android:layout_below="@+id/title" 
    android:layout_toRightOf="@+id/icon" 
    android:layout_toEndOf="@+id/icon" 
    android:layout_toLeftOf="@+id/pausePlay" 
    android:layout_toStartOf="@+id/pausePlay"/> 

<ImageButton 
    android:layout_width="40dp" 
    android:layout_height="40dp" 
    android:id="@+id/pausePlay" 
    android:scaleType="fitXY" 
    android:background="#e6e6e6" 
    android:layout_centerVertical="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

</RelativeLayout> 

wv.setWebChromeClient(new webChromeClient());  
wv.addJavascriptInterface(new JSInterface(), "JSOUT"); 

이 </활동에서 매니페스트에 이것을 추가 웹뷰이 추가>

<receiver android:name=".NotificationPausePlay" />