Now what is happening is it is also controlling other music players. If i have a default music player after starting that if i start playing using my music player and then i press play/pause button in headset it controls default music player.
i have implemented a heaset broadcastreceiver like this,
switch (keyEvent.getKeyCode()) {
case KeyEvent.KEYCODE_HEADSETHOOK:
Log.d("TAG", "TAG: KEYCODE_HEADSETHOOK");
if (MusicService.isPlaying()) {
MusicService.player.pause();
} else {
MusicService.player.start();
}
new MusicService().showNotification();
break;
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
Log.d("TAG", "TAG: KEYCODE_MEDIA_PLAY");
if (MusicService.isPlaying()) {
MusicService.player.pause();
} else {
MusicService.player.start();
}
new MusicService().showNotification();
break;
case KeyEvent.KEYCODE_MEDIA_PLAY:
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
Log.d("TAG", "TAG: KEYCODE_MEDIA_PAUSE");
break;
case KeyEvent.KEYCODE_MEDIA_STOP:
break;
case KeyEvent.KEYCODE_MEDIA_NEXT:
Log.d("TAG", "TAG: KEYCODE_MEDIA_NEXT");
if (MusicService.player.isPlaying()) {
MusicService.player.stop();
}
if (MusicService.songPosn < (MusicService.song.size() - 1)) {
MusicService.songPosn = MusicService.songPosn + 1;
if (SingleInstance.getInstance().getIndexCount()>= 1)
SingleInstance.getInstance().setIndexCount(SingleInstance.getInstance().getIndexCount()-1);
} else {
// play first song
MusicService.songPosn = 0;
}
new MusicService().playSong(true);
break;
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
Log.d("TAG", "TAG: KEYCODE_MEDIA_PREVIOUS");
if (MusicService.player.isPlaying()) {
MusicService.player.stop();
}
if (MusicService.songPosn > 0) {
MusicService.songPosn = MusicService.songPosn - 1;
} else {
// play last song
MusicService.songPosn = MusicService.song.size() - 1;
}
new MusicService().playSong(true);
break;
}
If you are trying to get the hedsethook event in a activity follow the below code :
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_HEADSETHOOK:
// Headset key detected
// Do your stuff here
return true;// To let the os know that the event is consumed here
}
}
return super.dispatchKeyEvent(event);
}
Make sure to returt true to consume the event .To get it done in a service use BroadCast receiver as follows:
IntentFilter filter = new IntentFilter(Intent.ACTION_MEDIA_BUTTON);//"android.intent.action.MEDIA_BUTTON"
MediaButtonIntentReceiver r = new MediaButtonIntentReceiver();
filter.setPriority(1000); //Sets receiver priority
registerReceiver(r, filter);
Currently I have a list of TextViews whose background get changed when click for the first time. But I also want when user clicks on the second time its background should again change and vice versa.
With my existing code I am only able to change background for the first click, when I am clicking it again its background is not changing.
Here is my code:
public void onClick(View v) {
switch (v.getId()) {
case R.id.goalText1:
if (count <= 2) {
goals.add(mGoal1.getText().toString());
mGoal1.setTextColor(getResources().getColor(R.color.black));
mGoal1.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText2:
if (count <= 2) {
goals.add(mGoal2.getText().toString());
mGoal2.setTextColor(getResources().getColor(R.color.black));
mGoal2.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText3:
if (count <= 2) {
goals.add(mGoal3.getText().toString());
mGoal3.setTextColor(getResources().getColor(R.color.black));
mGoal3.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText4:
if (count <= 2) {
goals.add(mGoal4.getText().toString());
mGoal4.setTextColor(getResources().getColor(R.color.black));
mGoal4.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText5:
if (count <= 2) {
goals.add(mGoal5.getText().toString());
mGoal5.setTextColor(getResources().getColor(R.color.black));
mGoal5.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText6:
if (count <= 2) {
goals.add(mGoal6.getText().toString());
mGoal6.setTextColor(getResources().getColor(R.color.black));
mGoal6.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.goalText7:
if (count <= 2) {
goals.add(mGoal7.getText().toString());
mGoal7.setTextColor(getResources().getColor(R.color.black));
mGoal7.setBackgroundColor(getResources().getColor(R.color.white));
count++;
} else {
Toast.makeText(SelectGoal.this, "cant select more", Toast.LENGTH_SHORT).show();
}
break;
case R.id.btnGoal:
Intent intent = new Intent(this, fiteness_level_selection.class);
try {
obj.put("SelectedGoal", goals);
} catch (Exception e) {
e.printStackTrace();
}
intent.putExtra("GoalJson", obj.toString());
startActivity(intent);
break;
}
So can anybody suggest me any easy way to achieve it.
Set a boolean array in your activity.
//suppose you've 3 textviews within listview
public boolean isTransparent[] = {false,false,false};
And then do this in your onClick method.
public void onOnclick(View v){
switch (v.getId()) {
//foreach textview do this
case R.id.textView1 :
int color;
if (isTransparent[0])
color = getResources().getColor(android.R.color.transparent);
else
color = getResources().getColor(R.color.white);
isTransparent[0]=!isTransparent[0];
textview1.setBackgroundColor(color);
break;
case R.id.textView2:
// now check for isTransparent[1]
and so on ...
...
}
}
As What I am understand is you want Change background for that you have take bool variable and in Button click you have to check the bool value.
Boolean click_firsttime = false;
Inside your Button click
use this way
If(click_firsttime == false){
//perform task
// and set bool value to true
click_firsttime = true;
}else{
// perform task and set bool value
click_firsttime = false;
}
First create a boolean :
private boolean foo = true;
Then change your code like this :
public void onClick(View v) {
switch (v.getId()) {
case R.id.goalText1:
if (foo) {
goals.add(mGoal1.getText().toString());
mGoal1.setTextColor(getResources().getColor(R.color.black));
mGoal1.setBackgroundColor(getResources().getColor(R.color.white));
count++;
foo = false;
} else {
// set background to another color
foo = true;
}
break;
The problem SoundPool when I call it in fragments onCreateView, onStart or onResume application to slow down.
And when I try to get the ID by pressing the button, the sound does not appear Logcat writes "SoundPool: sample 1 not READY"
please help me,Any ideas? Thanks!
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_carnivore, container, false);
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
Sound.createOldSoundPool();
} else {
Sound.createNewSoundPool();
}
mAlligatorSound = Sound.loadSound(getContext(),"alligator.wav");
mBatSound = Sound.loadSound(getContext(), "bat.mp3");
mBearSound = Sound.loadSound(getContext(),"bear.wav");
mBobcatSound=Sound.loadSound(getContext(),"bobcat.mp3");
mCatSound= Sound.loadSound(getContext(),"cat.wav");
mCheetahSound= Sound.loadSound(getContext(),"cheetah.wav");
mCoyoteSound= Sound.loadSound(getContext(),"coyote.wav");
mCrocodileSound= Sound.loadSound(getContext(),"crocodile.wav");
mDogSound= Sound.loadSound(getContext(),"dog.wav");
mFoxSound= Sound.loadSound(getContext(),"fox.mp3");
mHyenaSound= Sound.loadSound(getContext(),"hyena.wav");
mJaguarSound= Sound.loadSound(getContext(),"jaguar.wav");
mKittenSound= Sound.loadSound(getContext(),"kitten.wav");
mLeopardSound= Sound.loadSound(getContext(),"leopard.wav");
mLionSound= Sound.loadSound(getActivity(),"lion.wav");
mPuppySound= Sound.loadSound(getActivity(),"puppy.wav");
mRattleSnakeSound= Sound.loadSound(getActivity(),"rattlesnake.mp3");
mSnakeSound= Sound.loadSound(getActivity(),"snake.wav");
mTigerSound= Sound.loadSound(getActivity(),"tiger.wav");
mWolfSound= Sound.loadSound(getActivity(),"wolf.wav");
int idList[]={R.id.imageButtonAlligator,R.id.imageButtonBat,R.id.imageButtonBear,R.id.imageButtonBobCat,R.id.imageButtonCat,
R.id.imageButtonCheetah,R.id.imageButtonCoyote,R.id.imageButtonCrocodile,R.id.imageButtonDog,R.id.imageButtonFox,
R.id.imageButtonHyena,R.id.imageButtonJaguar,R.id.imageButtonKitten,R.id.imageButtonLeopard,R.id.imageButtonLion,
R.id.imageButtonPuppy,R.id.imageButtonRattlesnake,R.id.imageButtonSnake,R.id.imageButtonTiger,R.id.imageButtonWolf};
for (int id :idList){
ImageButton imageButton = (ImageButton)view.findViewById(id);
imageButton.setOnClickListener(onClickListener);
}
return view;
}
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButtonAlligator:
Sound.playSound(mAlligatorSound);
Toast.makeText(view.getContext(),R.string.Alligator, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBat:
Sound.playSound(mBatSound);
Toast.makeText(view.getContext(),R.string.Bat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBear:
Sound.playSound(mBearSound);
Toast.makeText(view.getContext(),R.string.Bear, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBobCat:
Sound.playSound(mBobcatSound);
Toast.makeText(view.getContext(),R.string.BobCat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCat:
Sound.playSound(mCatSound);
Toast.makeText(view.getContext(),R.string.Cat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCheetah:
Sound.playSound(mCheetahSound);
Toast.makeText(view.getContext(),R.string.Cheetah, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCoyote:
Sound.playSound(mCoyoteSound);
Toast.makeText(view.getContext(),R.string.Coyote, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCrocodile:
Sound.playSound(mCrocodileSound);
Toast.makeText(view.getContext(),R.string.Crocodile, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonDog:
Sound.playSound(mDogSound);
Toast.makeText(view.getContext(),R.string.Dog, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonFox:
Sound.playSound(mFoxSound);
Toast.makeText(view.getContext(),R.string.Fox, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonHyena:
Sound.playSound(mHyenaSound);
Toast.makeText(view.getContext(),R.string.Hyena, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonJaguar:
Sound.playSound(mJaguarSound);
Toast.makeText(view.getContext(),R.string.Jaguar, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonKitten:
Sound.playSound(mKittenSound);
Toast.makeText(view.getContext(),R.string.Kitten, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonLeopard:
Sound.playSound(mLeopardSound);
Toast.makeText(view.getContext(),R.string.Leopard, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonLion:
Sound.playSound(mLionSound);
Toast.makeText(view.getContext(),R.string.Lion, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonPuppy:
Sound.playSound(mPuppySound);
Toast.makeText(view.getContext(),R.string.Puppy, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonRattlesnake:
Sound.playSound(mRattleSnakeSound);
Toast.makeText(view.getContext(),R.string.RattleSnake, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonSnake:
Sound.playSound(mSnakeSound);
Toast.makeText(view.getContext(),R.string.Snake, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonTiger:
Sound.playSound(mTigerSound);
Toast.makeText(view.getContext(),R.string.Tiger, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonWolf:
Sound.playSound(mWolfSound);
Toast.makeText(view.getContext(),R.string.Wolf, Toast.LENGTH_SHORT).show();
break;
}
}
};
When you click on the sound does not work when the method is called Sound.loadSound (..., ...), and Logcat writes "SoundPool: sample 1 not READY"
switch (v.getId()) {
case R.id.imageButtonAlligator:
mAlligatorSound = Sound.loadSound(getContext(),"alligator.wav");
Sound.playSound(mAlligatorSound);
Toast.makeText(view.getContext(),R.string.Alligator, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBat:
mBatSound = Sound.loadSound(getContext(), "bat.mp3");
Sound.playSound(mBatSound);
Toast.makeText(view.getContext(),R.string.Bat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBear:
mBearSound = Sound.loadSound(getContext(),"bear.wav");
Sound.playSound(mBearSound);
Toast.makeText(view.getContext(),R.string.Bear, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonBobCat:
mBobcatSound=Sound.loadSound(getContext(),"bobcat.mp3");
Sound.playSound(mBobcatSound);
Toast.makeText(view.getContext(),R.string.BobCat, Toast.LENGTH_SHORT).show();
break;
case R.id.imageButtonCat:
mCatSound= Sound.loadSound(getContext(),"cat.wav");
Sound.playSound(mCatSound);
Toast.makeText(view.getContext(),R.string.Cat, Toast.LENGTH_SHORT).show();
break;
this is th Static Method Sound
public class Sound {
public static SoundPool mSoundPool;
public static AssetManager mAssetManager;
public static int mStreamID;
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
public static void createNewSoundPool() {
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
mSoundPool = new SoundPool.Builder()
.setAudioAttributes(attributes)
.build();
}
#SuppressWarnings("deprecation")
public static void createOldSoundPool() {
mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
}
public static int playSound(int sound) {
if (sound > 0) {
mStreamID = mSoundPool.play(sound, 1, 1, 1, 0, 1);
}
return mStreamID;
}
public static int loadSound(Context context , String fileName) {
mAssetManager = context.getAssets();
AssetFileDescriptor afd;
try {
afd = mAssetManager.openFd(fileName);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(context, "Не могу загрузить файл " + fileName,
Toast.LENGTH_SHORT).show();
return -1;
}
return mSoundPool.load(afd, 1);
}
}
Before doing anything with the soundpool, after you instantiate it, you need to set a listener to know when its actually ready to be used, it takes time to load the sound into memory: do this:
boolean loaded = false;//class variable
//in onCreate:
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
#Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
loaded = true;
}
});
then before you use the soundPool you need to check if Loaded is true.
For example:
if (loaded) {
soundPool.play(soundID, volume, volume, 1, 0, 1f);
Log.e("Test", "Played sound");
}
I have an ASyncTask which is running the update for my UserInterface; but I am not sure how I can access my AnalogInput[] parameter and check which index I am currently on for when I call my publishProgress();
private class AnalogUpdater extends AsyncTask<AnalogInput[], Float, Void> {
// A callback method executed on non UI thread, invoked after
// onPreExecute method if exists
// Takes a set of parameters of the type defined in your class
// implementation. This method will be
// executed on the background thread, so it must not attempt to interact
// with UI objects.
#Override
protected Void doInBackground(AnalogInput[]... params) {
try {
for (int i = 0; i < 15; i++) {
publishProgress(params[0][i].getVoltage());
}
} catch (ConnectionLostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onProgressUpdate(Float... f) {
// Getting reference to the TextView tv_counter of the layout
// activity_main
final String str = String.format("Voltage: %.2f", f);
final int v = Math.round(f[0] * 100);
switch (idx // How can I get this index?) {
case 1:
analogInput0VoltageText.setText(str);
analogInput0VoltageBar.setProgress(v);
break;
case 2:
analogInput1VoltageText.setText(str);
analogInput1VoltageBar.setProgress(v);
break;
case 3:
analogInput2VoltageText.setText(str);
analogInput2VoltageBar.setProgress(v);
break;
case 4:
analogInput3VoltageText.setText(str);
analogInput3VoltageBar.setProgress(v);
break;
case 5:
analogInput4VoltageText.setText(str);
analogInput4VoltageBar.setProgress(v);
break;
case 6:
analogInput5VoltageText.setText(str);
analogInput5VoltageBar.setProgress(v);
break;
case 7:
analogInput6VoltageText.setText(str);
analogInput6VoltageBar.setProgress(v);
break;
case 8:
analogInput7VoltageText.setText(str);
analogInput7VoltageBar.setProgress(v);
break;
case 9:
analogInput8VoltageText.setText(str);
analogInput8VoltageBar.setProgress(v);
break;
case 10:
analogInput9VoltageText.setText(str);
analogInput9VoltageBar.setProgress(v);
break;
case 11:
analogInput10VoltageText.setText(str);
analogInput10VoltageBar.setProgress(v);
break;
case 12:
analogInput11VoltageText.setText(str);
analogInput11VoltageBar.setProgress(v);
break;
case 13:
analogInput12VoltageText.setText(str);
analogInput12VoltageBar.setProgress(v);
break;
case 14:
analogInput13VoltageText.setText(str);
analogInput13VoltageBar.setProgress(v);
break;
case 15:
analogInput14VoltageText.setText(str);
analogInput14VoltageBar.setProgress(v);
break;
case 16:
analogInput15VoltageText.setText(str);
analogInput15VoltageBar.setProgress(v);
break;
}
}
Hopefully this is clear; refer to the comment "How can I get this index?" in the code.
}
You could use an array as the second parameter on your asyncTask (might not be necessary, I don't really remember)
private class AnalogUpdater extends AsyncTask<AnalogInput[], Float[], Void>
Pass both values, voltage and index (cast index to float)
publishProgress(params[0][i].getVoltage(), (float)i);
And use it later (cast to int again)
protected void onProgressUpdate(Float... f)
{
idx = (int)f[1];
}
my code is:
android.location.GpsStatus.Listener gpsstatusListenerGps = new android.location.GpsStatus.Listener() {
public void onGpsStatusChanged(int event) {
GpsStatus gpsStatus = lm.getGpsStatus(null);
switch (event) {
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
locationResult.gotSatellites(gpsStatus);
Log.e("gpsStatus", "gpsStatus");
lm.removeGpsStatusListener(gpsstatusListenerGps);
for (GpsSatellite sat:lm.getGpsStatus(null).getSatellites()) {
Log.e("STATUS", "gpsx.GpsStatus.Sat.fixed: " + sat.usedInFix());
}
break;
case GpsStatus.GPS_EVENT_FIRST_FIX:
locationResult.gotSatellites(gpsStatus);
Log.e("FIX", "gpsx.fixed.");
lm.removeGpsStatusListener(gpsstatusListenerGps);
for (GpsSatellite sat:lm.getGpsStatus(null).getSatellites()) {
Log.e("FIXX", "gpsx.GpsStatus.Sat.fixed: " + sat.usedInFix());
}
break;
}
}
};
...
Log.e("sat.hasNext()",""+sat.hasNext());
while (sat.hasNext()){
GpsSatellite oSat = (GpsSatellite) sat.next();
Log.e("gps",""+oSat.getSnr());
}
sat.hasNext is always false.
i always getting nullstring, so i cant get gpsstatuses, why?