我已经设置了一个广播,我想处理交换事件。当我没有设置任何服务时,交换事件工作正常,但现在意图中似乎有一个空点异常,因此交换事件没有得到正确处理。
错误在这一行:
intentswap.putExtra("swap",-1 );
sendBroadcast(intentswap);
我的活动代码
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.now_playing);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Intent i = getIntent();
position = i.getIntExtra("Data2", 0);
songdetails = getIntent().getParcelableArrayListExtra("Data1");
//intentswap=new Intent(BROADCAST_SWAP);
bfake=(Button)findViewById(R.id.bFake);
try
{ intentplaypause=new Intent(BROADCAST_PLAYPAUSE);
intent = new Intent(BROADCAST_SEEKBAR);
serviceIntent = new Intent(this, Music_service.class);
initViews();
setListeners();
}
catch(Exception e)
{}
playAudio(position);
buttonPlayStop.setBackgroundResource(R.drawable.pause);
boolMusicPlaying = true;
}
private void setListeners() {
bfake.setOnTouchListener(new OnSwipeTouchListener()
{
@Override
public void onSwipeRight()
{ z=-1;
intentswap=new Intent(BROADCAST_SWAP);
intentswap.putExtra("swap",-1 );
sendBroadcast(intentswap);
}
@Override
public void onSwipeLeft() {
z=1;
intentswap=new Intent(BROADCAST_SWAP);
intentswap.putExtra("swap",1 );
sendBroadcast(intentswap);
}
}
);
buttonPlayStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
buttonPlayStopClick();
}
});
seekbar.setOnSeekBarChangeListener(this);
}
// not working correctly,also i have to add pause and not stop
private void buttonPlayStopClick() {
if (!boolMusicPlaying) {
buttonPlayStop.setBackgroundResource(R.drawable.pause);
playAudio(position);
boolMusicPlaying = true;
} else {
if (boolMusicPlaying) {
buttonPlayStop.setBackgroundResource(R.drawable.play);
stopMusic_service();
boolMusicPlaying = false;
}
}
}
private void playAudio(int position2) {
position2 = position;
serviceIntent.putParcelableArrayListExtra("sentAudioLink", songdetails);
serviceIntent.putExtra("postion_service", position);
try {
startService(serviceIntent);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
e.getClass().getName() + " " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
registerReceiver(broadcastReceiver, new IntentFilter(
Music_service.BROADCAST_ACTION));
mBroadcastIsRegistered = true;
}
private void stopMusic_service() {
if (mBroadcastIsRegistered) {
try {
// unregisterReceiver(broadcastReceiver);
// mBroadcastIsRegistered = false;
} catch (Exception e) {
// Log.e(TAG, "Error in Activity", e);
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(
getApplicationContext(),
e.getClass().getName() + " " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
try {
stopService(serviceIntent);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
e.getClass().getName() + " " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
boolMusicPlaying = false;
}
private void initViews() {
buttonPlayStop = (Button) findViewById(R.id.bPlayPause);
buttonPlayStop.setBackgroundResource(R.drawable.play);
seekbar = (SeekBar) findViewById(R.id.songProgressBar);
}
@Override
public void onProgressChanged(SeekBar sb, int progress,
boolean fromUser) {
if (fromUser) {
int seekPos = sb.getProgress();
intent.putExtra("seekpos", seekPos);
sendBroadcast(intent);
}
}
private void showPD(Intent bufferIntent) {
String bufferValue = bufferIntent.getStringExtra("buffering");
int bufferIntValue = Integer.parseInt(bufferValue);
// When the broadcasted "buffering" value is 1, show "Buffering"
// progress dialogue.
// When the broadcasted "buffering" value is 0, dismiss the progress
// dialogue.
switch (bufferIntValue) {
// Listen for "2" to reset the button to a play button
case 2:
buttonPlayStop.setBackgroundResource(R.drawable.play);
break;
}
}
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent serviceIntent) {
updateUI(serviceIntent);
}
};
private void updateUI(Intent serviceIntent) {
String counter = serviceIntent.getStringExtra("counter");
String mediamax = serviceIntent.getStringExtra("mediamax");
String strSongEnded = serviceIntent.getStringExtra("song_ended");
int seekProgress = Integer.parseInt(counter);
seekMax = Integer.parseInt(mediamax);
songEnded = Integer.parseInt(strSongEnded);
seekbar.setMax(seekMax);
seekbar.setProgress(seekProgress);
if (songEnded == 1) {
buttonPlayStop.setBackgroundResource(R.drawable.play);
}
}
private BroadcastReceiver broadcastBufferReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent bufferIntent) {
showPD(bufferIntent);
}
};
@Override
protected void onPause() {
// Unregister broadcast receiver
try{ if (mBroadcastIsRegistered) {
unregisterReceiver(broadcastBufferReceiver);
mBroadcastIsRegistered = false;
}}catch (Exception e){}
super.onPause();
}
// -- onResume register broadcast receiver. To improve, retrieve saved
// screen data ---
@Override
protected void onResume() {
// Register broadcast receiver
if (!mBroadcastIsRegistered) {
registerReceiver(broadcastBufferReceiver, new IntentFilter(
Music_service.BROADCAST_BUFFER));
mBroadcastIsRegistered = true;
}
super.onResume();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
日志猫
10-11 01:52:18.734: W/System.err(561): at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:294)
10-11 01:52:18.734: W/System.err(561): at source.justanothermusicplayer.NowPlaying$3.onSwipeLeft(NowPlaying.java:88)
10-11 01:52:18.744: W/System.err(561): at source.justanothermusicplayer.classes.OnSwipeTouchListener$GestureListener.onFling(OnSwipeTouchListener.java:45)
10-11 01:52:18.744: W/System.err(561): at android.view.GestureDetector.onTouchEvent(GestureDetector.java:579)
10-11 01:52:18.744: W/System.err(561): at source.justanothermusicplayer.classes.OnSwipeTouchListener.onTouch(OnSwipeTouchListener.java:16)
10-11 01:52:18.744: W/System.err(561): at android.view.View.dispatchTouchEvent(View.java:5481)
10-11 01:52:18.744: W/System.err(561): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
10-11 01:52:18.744: W/System.err(561): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
10-11 01:52:18.754: W/System.err(561): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
10-11 01:52:18.764: W/System.err(561): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
10-11 01:52:18.764: W/System.err(561): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
10-11 01:52:18.764: W/System.err(561): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
10-11 01:52:18.764: W/System.err(561): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1959)
10-11 01:52:18.764: W/System.err(561): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1728)
10-11 01:52:18.774: W/System.err(561): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1892)
10-11 01:52:18.774: W/System.err(561): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1371)
10-11 01:52:18.774: W/System.err(561): at android.app.Activity.dispatchTouchEvent(Activity.java:2364)
10-11 01:52:18.784: W/System.err(561): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1840)
10-11 01:52:18.794: W/System.err(561): at android.view.View.dispatchPointerEvent(View.java:5662)
10-11 01:52:18.794: W/System.err(561): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:2863)
10-11 01:52:18.794: W/System.err(561): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
10-11 01:52:18.814: W/System.err(561): at android.os.Handler.dispatchMessage(Handler.java:99)
10-11 01:52:18.814: W/System.err(561): at android.os.Looper.loop(Looper.java:137)
10-11 01:52:18.824: W/System.err(561): at android.app.ActivityThread.main(ActivityThread.java:4340)
10-11 01:52:18.824: W/System.err(561): at java.lang.reflect.Method.invokeNative(Native Method)
10-11 01:52:18.824: W/System.err(561): at java.lang.reflect.Method.invoke(Method.java:511)
10-11 01:52:18.834: W/System.err(561): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-11 01:52:18.834: W/System.err(561): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-11 01:52:18.834: W/System.err(561): at dalvik.system.NativeStart.main(Native Method)