Which is better? To instantiate a Broadcast receiver inside an Activity or in Service class, or make a class that extends BroadcastReceiver?
Below is an example where I instantiate BroadcastReceiver inside a Service class.
public BroadcastReceiver receiver = new BroadcastReceiver() {
private String filename;
@Override
public void onReceive(Context context, Intent intent){
String action = intent.getAction();
Bundle extras = intent.getExtras();
filename = extras.getString("AudioPath");
Toast.makeText(AudioService.this, "the audio file name sent: " + filename , Toast.LENGTH_LONG).show();
if(action.equals("com.porno.xxx.AudioPlay")){
selectedAudioPath = audiopath;
String state = intent.getExtras().getString("stringdata");
playSong();
Toast.makeText(AudioService.this, "play audio from service string data "+ state, Toast.LENGTH_LONG).show();
}
else if(action.equals("com.porno.xxx.AudioPause")){
pauseSong();
selectedAudioPath = audiopath;
Toast.makeText(AudioService.this, "pause audio from service", Toast.LENGTH_LONG).show();
}
else if(action.equals("com.porno.xxx.AudioSelector")){
Toast.makeText(AudioService.this, "music selector from service", Toast.LENGTH_LONG).show();
Intent i = new Intent();
audiopath = intent.getStringExtra("filename");
Toast.makeText(AudioService.this, "selelcted audio path: " + audiopath, Toast.LENGTH_LONG).show();
}
else if(action.equals("com.porno.xxx.AudioRelease")){
Toast.makeText(AudioService.this, "My Service Stopped and destoryed", Toast.LENGTH_LONG).show();
player.stop();
if (player != null) player.release();
}
}
};