嗨,我有我的应用程序 aacplayer,但我试图实现一个选项卡菜单,我实现了这一点,但是当我的应用程序打开时,我的问题在这里必须运行 MainActivity.java 但是那个有音频服务,但我添加了另一个选项卡并且工作完美,那些没有使用服务。
这是错误日志:
02-14 19:24:11.685: E/AndroidRuntime(31027): java.lang.RuntimeException: Error receiving broadcast Intent { act=STARTED flg=0x10 } in com.webcraftbd.radio.MainActivity$RadioUpdateReceiver@42e09468
tabactivity.java:
public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("En Vivo");
photospec.setIndicator("En Vivo", getResources().getDrawable(R.drawable.icon_songs_tab));
Intent photosIntent = new Intent(this, MainActivity.class);
photospec.setContent(photosIntent);
// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Twitter");
// setting Title and Icon for the Tab
songspec.setIndicator("Twitter", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent songsIntent = new Intent(this, SongsActivity.class);
songspec.setContent(songsIntent);
// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Contactenos");
videospec.setIndicator("Contactenos", getResources().getDrawable(R.drawable.icon_videos_tab));
Intent videosIntent = new Intent(this, VideosActivity.class);
videospec.setContent(videosIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
{
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.WHITE);
tv.setPadding(0, 0, 0, 5);
tv.setShadowLayer(2, 2, 2, Color.BLACK);
}
}
}
MainActivity.java:
public class MainActivity extends BaseActivity{
private static boolean displayAd;
private Button playButton;
private Button pauseButton;
private Button stopButton;
private Button nextButton;
private Button previousButton;
private ImageView stationImageView;
private TextView albumTextView;
private TextView artistTextView;
private TextView trackTextView;
private TextView statusTextView;
private TextView timeTextView;
private Intent bindIntent;
private TelephonyManager telephonyManager;
private boolean wasPlayingBeforePhoneCall = false;
private RadioUpdateReceiver radioUpdateReceiver;
private RadioService radioService;
private AdView adView;
private String STATUS_BUFFERING;
private static final String TYPE_AAC = "aac";
private static final String TYPE_MP3 = "mp3";
private SeekBar volumeSeekbar = null;
private AudioManager audioManager = null;
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.activity_main);
initControls();
// Bind to the service
try {
bindIntent = new Intent(this, RadioService.class);
bindService(bindIntent, radioConnection, Context.BIND_AUTO_CREATE);
}
catch(Exception e) {
}
telephonyManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(telephonyManager != null) {
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
handler = new Handler();
initialize();
}
private void initControls() {
try {
volumeSeekbar = (SeekBar) findViewById(R.id.seekBar1);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
volumeSeekbar.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC));
volumeSeekbar
.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onProgressChanged(SeekBar arg0,
int progress, boolean arg2) {
audioManager.setStreamVolume(
AudioManager.STREAM_MUSIC, progress, 0);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
int index = volumeSeekbar.getProgress();
volumeSeekbar.setProgress(index + 1);
return false;
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
int index = volumeSeekbar.getProgress();
volumeSeekbar.setProgress(index - 1);
return false;
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT || newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.activity_main);
try {
handler.post( new Runnable() {
public void run() {
initialize();
if(radioService.getTotalStationNumber()<=1) {
nextButton.setEnabled(false);
nextButton.setVisibility(View.INVISIBLE);
previousButton.setEnabled(false);
previousButton.setVisibility(View.INVISIBLE);
}
updateStatus();
updateMetadata();
//updateAlbum();
System.out.println("radioService.isPreparingStarted() = "+radioService.isPreparingStarted());
}
});
}
catch(Exception e) {
e.printStackTrace();
}
}
}
public void initialize() {
try {
displayAd = (boolean) Boolean.parseBoolean(getResources().getString(R.string.is_display_ad));
STATUS_BUFFERING = getResources().getString(R.string.status_buffering);
playButton = (Button) this.findViewById(R.id.PlayButton);
pauseButton = (Button) this.findViewById(R.id.PauseButton);
stopButton = (Button) this.findViewById(R.id.StopButton);
nextButton = (Button) this.findViewById(R.id.NextButton);
previousButton = (Button) this.findViewById(R.id.PreviousButton);
pauseButton.setEnabled(false);
pauseButton.setVisibility(View.INVISIBLE);
stationImageView = (ImageView) findViewById(R.id.stationImageView);
playButton.setEnabled(true);
stopButton.setEnabled(false);
albumTextView = (TextView) this.findViewById(R.id.albumTextView);
artistTextView = (TextView) this.findViewById(R.id.artistTextView);
trackTextView = (TextView) this.findViewById(R.id.trackTextView);
statusTextView = (TextView) this.findViewById(R.id.statusTextView);
timeTextView = (TextView) this.findViewById(R.id.timeTextView);
startService(new Intent(this, RadioService.class));
displayAd();
} catch (Exception e) {
e.printStackTrace();
}
}
public void displayAd() {
if(displayAd==true) {
// Create the adView
try {
if (adView != null) {
adView.destroy();
}
adView = new AdView(this, AdSize.SMART_BANNER, this.getString(R.string.admob_publisher_id));
LinearLayout layout = (LinearLayout)findViewById(R.id.adLayout);
layout.addView(adView);
adView.loadAd(new AdRequest());
} catch (OutOfMemoryError e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
else {
LinearLayout layout = (LinearLayout)findViewById(R.id.adLayout);
layout.setLayoutParams(new LinearLayout.LayoutParams(0,0));
layout.setVisibility(View.INVISIBLE);
}
}
public void updatePlayTimer() {
timeTextView.setText(radioService.getPlayingTime());
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doAsynchronousTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
public void run() {
timeTextView.setText(radioService.getPlayingTime());
}
});
}
};
timer.schedule(doAsynchronousTask, 0, 1000);
}
public void onClickPlayButton(View view) {
radioService.play();
}
public void onClickPauseButton(View view) {
radioService.pause();
}
public void onClickStopButton(View view) {
radioService.stop();
//resetMetadata();
//updateDefaultCoverImage();
}
public void onClickNextButton(View view) {
resetMetadata();
playNextStation();
//updateDefaultCoverImage();
}
public void onClickPreviousButton(View view) {
resetMetadata();
playPreviousStation();
//updateDefaultCoverImage();
}
public void playNextStation() {
radioService.stop();
radioService.setNextStation();
/*
if(radioService.isPlaying()) {
radioService.setStatus(STATUS_BUFFERING);
updateStatus();
radioService.stop();
radioService.play();
}
else {
radioService.stop();
}
*/
}
public void playPreviousStation() {
radioService.stop();
radioService.setPreviousStation();
/*
if(radioService.isPlaying()) {
radioService.setStatus(STATUS_BUFFERING);
updateStatus();
radioService.stop();
radioService.play();
}
else {
radioService.stop();
}
*/
}
public void updateDefaultCoverImage() {
String mDrawableName = "station_"+(radioService.getCurrentStationID()+1);
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
stationImageView.setImageResource(resID);
albumTextView.setText("");
}
public void updateAlbum() {
String album = radioService.getAlbum();
String artist = radioService.getArtist();
String track = radioService.getTrack();
Bitmap albumCover = radioService.getAlbumCover();
albumTextView.setText(album);
if(albumCover==null || (artist.equals("") && track.equals("")))
updateDefaultCoverImage();
else {
stationImageView.setImageBitmap(albumCover);
radioService.setAlbum(LastFMCover.album);
if(radioService.getAlbum().length() + radioService.getArtist().length()>50) {
albumTextView.setText("");
}
}
}
public void updateMetadata() {
String artist = radioService.getArtist();
String track = radioService.getTrack();
//if(artist.length()>30)
//artist = artist.substring(0, 30)+"...";
artistTextView.setText(artist);
trackTextView.setText(track);
albumTextView.setText("");
}
public void resetMetadata() {
radioService.resetMetadata();
artistTextView.setText("");
albumTextView.setText("");
trackTextView.setText("");
}
@Override
public void onDestroy() {
super.onDestroy();
if(radioService!=null) {
if(!radioService.isPlaying() && !radioService.isPreparingStarted()) {
//radioService.stopSelf();
radioService.stopService(bindIntent);
}
}
if (adView != null) {
adView.destroy();
}
if(telephonyManager != null) {
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
}
}
@Override
protected void onPause() {
super.onPause();
if (radioUpdateReceiver != null)
unregisterReceiver(radioUpdateReceiver);
}
@Override
protected void onResume() {
super.onResume();
/* Register for receiving broadcast messages */
if (radioUpdateReceiver == null) radioUpdateReceiver = new RadioUpdateReceiver();
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_CREATED));
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_DESTROYED));
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_STARTED));
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_START_PREPARING));
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_PREPARED));
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_PLAYING));
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_PAUSED));
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_STOPPED));
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_COMPLETED));
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_ERROR));
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_BUFFERING_START));
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_BUFFERING_END));
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_METADATA_UPDATED));
registerReceiver(radioUpdateReceiver, new IntentFilter(RadioService.MODE_ALBUM_UPDATED));
if(wasPlayingBeforePhoneCall) {
radioService.play();
wasPlayingBeforePhoneCall = false;
}
}
/* Receive Broadcast Messages from RadioService */
private class RadioUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(RadioService.MODE_CREATED)) {
}
else if (intent.getAction().equals(RadioService.MODE_DESTROYED)) {
playButton.setEnabled(true);
pauseButton.setEnabled(false);
stopButton.setEnabled(false);
playButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.INVISIBLE);
//updateDefaultCoverImage();
updateMetadata();
updateStatus();
}
else if (intent.getAction().equals(RadioService.MODE_STARTED)) {
pauseButton.setEnabled(false);
playButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.INVISIBLE);
playButton.setEnabled(true);
stopButton.setEnabled(false);
updateStatus();
}
else if (intent.getAction().equals(RadioService.MODE_START_PREPARING)) {
pauseButton.setEnabled(false);
playButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.INVISIBLE);
playButton.setEnabled(false);
stopButton.setEnabled(true);
updateStatus();
}
else if (intent.getAction().equals(RadioService.MODE_PREPARED)) {
playButton.setEnabled(true);
pauseButton.setEnabled(false);
stopButton.setEnabled(false);
playButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.INVISIBLE);
updateStatus();
}
else if (intent.getAction().equals(RadioService.MODE_BUFFERING_START)) {
updateStatus();
}
else if (intent.getAction().equals(RadioService.MODE_BUFFERING_END)) {
updateStatus();
}
else if (intent.getAction().equals(RadioService.MODE_PLAYING)) {
if(radioService.getCurrentStationType().equals(TYPE_AAC)) {
playButton.setEnabled(false);
stopButton.setEnabled(true);
}
else {
playButton.setEnabled(false);
pauseButton.setEnabled(true);
stopButton.setEnabled(true);
playButton.setVisibility(View.INVISIBLE);
pauseButton.setVisibility(View.VISIBLE);
}
updateStatus();
}
else if(intent.getAction().equals(RadioService.MODE_PAUSED)) {
playButton.setEnabled(true);
pauseButton.setEnabled(false);
stopButton.setEnabled(true);
playButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.INVISIBLE);
updateStatus();
}
else if(intent.getAction().equals(RadioService.MODE_STOPPED)) {
playButton.setEnabled(true);
pauseButton.setEnabled(false);
stopButton.setEnabled(false);
playButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.INVISIBLE);
updateStatus();
}
else if(intent.getAction().equals(RadioService.MODE_COMPLETED)) {
playButton.setEnabled(true);
pauseButton.setEnabled(false);
stopButton.setEnabled(false);
playButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.INVISIBLE);
updateStatus();
}
else if(intent.getAction().equals(RadioService.MODE_ERROR)) {
playButton.setEnabled(true);
pauseButton.setEnabled(false);
stopButton.setEnabled(false);
playButton.setVisibility(View.VISIBLE);
pauseButton.setVisibility(View.INVISIBLE);
updateStatus();
}
else if(intent.getAction().equals(RadioService.MODE_METADATA_UPDATED)) {
updateMetadata();
updateStatus();
//updateDefaultCoverImage();
}
else if(intent.getAction().equals(RadioService.MODE_ALBUM_UPDATED)) {
//updateAlbum();
}
}
}
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
wasPlayingBeforePhoneCall = radioService.isPlaying();
radioService.stop();
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
if(wasPlayingBeforePhoneCall) {
radioService.play();
}
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
//A call is dialing, active or on hold
wasPlayingBeforePhoneCall = radioService.isPlaying();
radioService.stop();
}
super.onCallStateChanged(state, incomingNumber);
}
};
public void updateStatus() {
String status = radioService.getStatus();
if(radioService.getTotalStationNumber() > 1) {
if(status!="")
status = radioService.getCurrentStationName()+" - "+status;
else
status = radioService.getCurrentStationName();
}
try {
statusTextView.setText(status);
}
catch(Exception e) {
e.printStackTrace();
}
}
// Handles the connection between the service and activity
private ServiceConnection radioConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
radioService = ((RadioService.RadioBinder)service).getService();
if(radioService.getTotalStationNumber()<=1) {
nextButton.setEnabled(false);
nextButton.setVisibility(View.INVISIBLE);
previousButton.setEnabled(false);
previousButton.setVisibility(View.INVISIBLE);
}
updateStatus();
updateMetadata();
//updateAlbum();
updatePlayTimer();
radioService.showNotification();
//radioService.play();
}
public void onServiceDisconnected(ComponentName className) {
radioService = null;
}
};
}
基本活动.java:
public class BaseActivity extends Activity {
private Intent bindIntent;
private RadioService radioService;
private static boolean isExitMenuClicked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isExitMenuClicked = false;
// Bind to the service
bindIntent = new Intent(this, RadioService.class);
bindService(bindIntent, radioConnection, Context.BIND_AUTO_CREATE);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
@Override
protected void onResume() {
super.onResume();
if(isExitMenuClicked==true)
finish();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent i;
final String thisClassName = this.getClass().getName();
final String thisPackageName = this.getPackageName();
switch (item.getItemId()) {
case R.id.radio:
if(!thisClassName.equals(thisPackageName+".MainActivity")) {
i = new Intent(this, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
return true;
}
break;
case R.id.facebook:
if(!thisClassName.equals(thisPackageName+".FacebookActivity")) {
i = new Intent(this, FacebookActivity.class);
startActivity(i);
return true;
}
break;
case R.id.twitter:
if(!thisClassName.equals(thisPackageName+".TwitterActivity")) {
i = new Intent(this, TwitterActivity.class);
startActivity(i);
return true;
}
break;
case R.id.about:
if(!thisClassName.equals(thisPackageName+".AboutActivity")) {
i = new Intent(this, AboutActivity.class);
startActivity(i);
return true;
}
break;
case R.id.exit:
String title = "Exit Radio";
String message = "Desea salir de la aplicacion?";
String buttonYesString = "Si";
String buttonNoString = "No";
isExitMenuClicked = true;
AlertDialog.Builder ad = new AlertDialog.Builder(this);
//ad.setTitle(title);
ad.setMessage(message);
ad.setCancelable(true);
ad.setPositiveButton(buttonYesString, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(radioService!=null) {
radioService.exitNotification();
radioService.stop();
radioService.stopService(bindIntent);
isExitMenuClicked = true;
finish();
}
}
});
ad.setNegativeButton(buttonNoString, null);
ad.show();
return true;
}
return super.onOptionsItemSelected(item);
}
// Handles the connection between the service and activity
private ServiceConnection radioConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
radioService = ((RadioService.RadioBinder)service).getService();
}
public void onServiceDisconnected(ComponentName className) {
radioService = null;
}
};
}
我不知道我还能做些什么来修复我的主要活动。
非常感谢您。