1

i just want to monitor all the traffic used by the MediaPlayer and show this Data Usage to User on TextView but this does not work only the First Time it return Some Bytes and not increment these bytes on Handler Thread While it increment a counter on the Handler

On Clicking the Start MediaPlayer Should be start and the Value of Data Usage should be increased as well while clicking on the stop MediaPlayer Stop and corresponding NetworkUsage Data increment should also be stop

This works with counter but not with mStartRx = TrafficStats.getUidRxBytes(Process.myUid());

Please Help Here is the code snippet i have tried on Android 2.2 API level-8

public class AndroidHandlerRunnableActivity extends Activity
{

 private int cnt = 0;
 Button Start, Stop;
 TextView counter;
 long mStartRx;
 private Handler handler = new Handler();
 private MediaPlayer mediaPlayer;
 ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        counter = (TextView)findViewById(R.id.counter);
        Start = (Button)findViewById(R.id.Start);
        Start.setEnabled(true);
        Stop = (Button)findViewById(R.id.Stop);
        Stop.setEnabled(false);

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnPreparedListener(new MediaOnPreparedListener());   
        String networkURL = "http://currentstream1.publicradio.org/";   
            try
            {
                mediaPlayer.setDataSource(networkURL);
            } 
            catch (Exception e)
            {           
                e.printStackTrace();
            }       
        mediaPlayer.prepareAsync();
        mStartRx =  TrafficStats.getUidRxBytes(Process.myUid());
        progressDialog = ProgressDialog.show(this, "Playing audio", "Please wait...."); 


        Start.setOnClickListener(new Button.OnClickListener()
        {
               @Override
               public void onClick(View v) 
               {            
                   handler.post(timedTask);
                   mediaPlayer.start();
                   Start.setEnabled(false);
                   Stop.setEnabled(true);               
               }
        });

        Stop.setOnClickListener(new Button.OnClickListener()
        {
             public void onClick(View v) 
             {          
                    handler.removeCallbacks(timedTask);
                    mediaPlayer.stop();
                    Start.setEnabled(true);
                    Stop.setEnabled(false);
              }
        });
}

    private class MediaOnPreparedListener implements MediaPlayer.OnPreparedListener
    {
        @Override
        public void onPrepared(MediaPlayer mp) 
        {       
            if (progressDialog != null) 
            {               
                progressDialog.dismiss();
            }               
        }       
    }

    private Runnable timedTask = new Runnable()
    {
            @Override
          public void run() 
          {     
               cnt++;
               mStartRx =  TrafficStats.getUidRxBytes(Process.myUid());
               counter.setText(String.valueOf(cnt)+" Data "+mStartRx);
               handler.postDelayed(timedTask, 2000);
          }
     };


        @Override
        protected void onStop() 
        {
            super.onStop();
            if (mediaPlayer != null)
            {           
                mediaPlayer.stop();
                mediaPlayer.reset();
                mediaPlayer.release();
            }
        }
}

For more help Here is My Layout File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

    <Button
        android:id="@+id/Start"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Start" />

    <Button
        android:id="@+id/Stop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Stop" />

    <TextView
        android:id="@+id/counter"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

And the manifiest file are as follows

   <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.exercise.AndroidHandlerRunnable"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".AndroidHandlerRunnableActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 Answers1

0

Here is the answer to your threading issue.

Furthermore, you should put all your ui stuff in onResume(), not in onCreate(). Trying to manipulate your UI before the UI thread has had a chance to get started (or before the UI thread has gotten a chance to get resumed) can be problematic.

And you should put the code that you're calling inside of onStop() inside of onPause() instead. onPause() is guaranteed to be called when the Activity is exited. onStop() is not. You can test for this use case when the user presses the home button for instance, or when the phone receives a phone call.

Here is some information about the Activity Lifecycle. It's worth giving that page a very close read.

Community
  • 1
  • 1
Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49