我只想监视 MediaPlayer 使用的所有流量,并在 TextView 上向用户显示此数据使用情况,但这仅在第一次返回一些字节时不起作用,并且不会在处理程序线程上增加这些字节,而它在处理程序上增加一个计数器
单击开始时 MediaPlayer 应该启动并且 Data Usage 的值也应该增加,同时单击停止 MediaPlayer Stop 并且相应的 NetworkUsage 数据增量也应该停止
这适用于计数器,但不适用于 mStartRx = TrafficStats.getUidRxBytes(Process.myUid());
请帮助这是我在 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();
}
}
}
如需更多帮助,这是我的布局文件
<?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>
清单文件如下
<?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>