How to use another class for the same layout

Asked

Viewed 161 times

0

It is as follows: I made adaptations in the codes in order to work the Slider Menu style so I had to use the Fragment interface, as shown in the code below, but in this same layout in this case is called the xml "fragment_listen", the problem is that I will use another interface to Acitivty for the functions in this same layout, I created a new class, but now I do not know for sure how I should or can do this to work.

Radiostation.java

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

@SuppressLint("NewApi")
public class RadioStation extends Fragment{
    public RadioStation(){}
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_listen, container, false);
        return rootView;
    }

}  

Listen.java

import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.media.AudioManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
public class Listen extends Activity
{
  protected static final String TAG = null;
  private AudioManager audioManager;
  private StreamingMediaPlayer audioStreamer;
  private boolean isPlaying;
  private ImageButton playButton;
  private String urlStreaming;
  private void definirVolumeSlider()
  {
    this.audioManager = ((AudioManager)getSystemService("audio"));
    int i = this.audioManager.getStreamMaxVolume(3);
    int j = this.audioManager.getStreamVolume(3);
    setVolumeControlStream(3);
    SeekBar localSeekBar = (SeekBar)findViewById(R.id.seekBar);
    localSeekBar.setMax(i);
    localSeekBar.setProgress(j);
    localSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener()
    {
      public void onProgressChanged(SeekBar paramAnonymousSeekBar, int paramAnonymousInt, boolean paramAnonymousBoolean)
      {
        Listen.this.audioManager.setStreamVolume(3, paramAnonymousInt, 0);
      }

      public void onStartTrackingTouch(SeekBar paramAnonymousSeekBar)
      {
      }

      public void onStopTrackingTouch(SeekBar paramAnonymousSeekBar)
      {
      }
    });
  }

  @SuppressLint({"NewApi"})
  private void notification()
  {
    NotificationManager localNotificationManager = (NotificationManager)getSystemService("notification");
    PendingIntent localPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, Listen.class), 0);
    localNotificationManager.notify(1, new Notification.Builder(this).setSmallIcon(2130837515).setContentTitle(" Rádio Sant'Ana").setContentText(" Essa rádio caiu do céu!").setOngoing(true).setContentIntent(localPendingIntent).build());
  }

  private void ouvir()
  {
    this.playButton = ((ImageButton)findViewById(R.id.button_play));
    this.playButton.setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramAnonymousView)
      {
        if (isPlaying)
        {
          audioStreamer.interrupt();
          playButton.setImageResource(R.drawable.button_play);
          ((NotificationManager)Listen.this.getSystemService("notification")).cancel(1);
        }
        if (!isPlaying)
        {
          startStreamingAudio();
          playButton.setImageResource(R.drawable.button_pause);
          notification();
        }
     isPlaying=!isPlaying;
      }
    });
  }

  private void sair()
  {
    if (this.audioStreamer != null)
      this.audioStreamer.interrupt();
    ((NotificationManager)getSystemService("notification")).cancel(1);
    onDestroy();
  }

  private void startStreamingAudio()
  {
    try
    {
      if (this.audioStreamer != null)
        this.audioStreamer.interrupt();
      this.audioStreamer = new StreamingMediaPlayer(this, playButton);
      this.audioStreamer.startStreaming(this.urlStreaming, 5208L, 216L);
      return;
    }
    catch (IOException localIOException)
    {
      while (true)
        Log.e(getClass().getName(), "Error starting to stream audio.", localIOException);
    }
  }

  public void onCreate()
  {
   ouvir();
    definirVolumeSlider();
    notification();
    sair();
    this.urlStreaming = "http://sh.upx.com.br:10369";
  }

  protected void onDestroy()
  {
    super.onDestroy();
    if (this.audioStreamer != null)
      this.audioStreamer.interrupt();
  }
}

The use of Activity is as for example the volume control through seekBar.

  • Not adding Layout by calling setContentView in the onCreate of Activity? I think you should focus all manipulation of View's to the Fragment and notify the Activity when an important event happens (change volume, play, stop, skip music and etc...). I think maybe one is missing Service there in the middle, but this can be left for later...

  • Because I put a 'view' on Radiostation. How it could be interacted with the same layout?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.