Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get seekbar in activity from mediaplayer service?
    primarykey
    data
    text
    <p>I'm creating a mediaplayer service to play a media file from internet, like this:</p> <pre><code>public class MyService extends Service{ private static final String TAG = "MyService"; MediaPlayer mediaPlayer; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); Log.d(TAG, "onCreate"); mediaPlayer = new MediaPlayer(); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); } @Override public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); mediaPlayer.stop(); } @Override public void onStart(Intent intent, int startid) { Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); String rtsp; Bundle re= intent.getExtras(); rtsp=re.getString("rtsp"); try { mediaPlayer.setDataSource(rtsp); mediaPlayer.prepare(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mediaPlayer.start(); } } </code></pre> <p>Now I want to get the seekbar from the activity like this:</p> <pre><code>SeekBar seekbar= (SeekBar)findviewbyid(R.id.seekbar); </code></pre> <p>But I can't :( Please help me how to do that!</p> <p><strong>EDIT</strong></p> <p>I found another way to solve my problem without using Bind service :) Here it is:</p> <p>In your service class, create a static method like this:</p> <pre><code>public static void setMainActivity(Player_Activity mainActivity) { MAIN_ACTIVITY=mainActivity; } </code></pre> <p>Ofcourse, you must declare a static varible:</p> <pre><code>private static Player_Activity MAIN_ACTIVITY; </code></pre> <p>Next, in your activity, before you call the startService(), you must call this method to set the main activity to your service like this:</p> <pre><code>MyService.setMainActivity(Player_Activity.this); Intent myservice= new Intent(this,MyService.class); startService(myservice); </code></pre> <p>And finally you can do anything with your activity in your service like this:</p> <pre><code>final TextView tv_test=(TextView)MAIN_ACTIVITY.findViewById(R.id.textview); </code></pre> <p>That's all! Hope this help :)</p> <p>Forgive me if I get some mistake about English ;), my English skill is not good :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload