i try to stop my sound in pressing back or home put it not work pleaze help me
this is the code
the sound play but i want the method to stop in pressing back or home.
public class Youcha3 extends Activity {
MediaPlayer mysound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youcha3);
Button btsound=(Button)findViewById(R.id.dou3aa);
btsound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mysound=MediaPlayer.create(Youcha3.this, R.raw.dou3aa );
if (mysound.isPlaying()) {
mysound.pause();
mysound.release();
}
else{
mysound.start();
}
}
});
}
Try to add this code in your Activity.
#Override
protected void onStop() {
if (mysound != null && mysound.isPlaying()) {
mysound.stop();
}
super.onPause();
}
i think this may help you.
try this
// back button press
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
if (mysound != null && mysound.isPlaying()) {
mysound.stop();
}
super.onBackPressed();
}
// home button press
#Override
public void onAttachedToWindow() {
// TODO Auto-generated method stub
if (mysound != null && mysound.isPlaying()) {
mysound.stop();
}
super.onAttachedToWindow();
}
Related
Ok, this one is really tricky to me. The idea of this is a time log for EMS. You press a button by the action taken, and it logs the time for later use. I managed to figure out how to save the TextView into a string. Now, how on earth do I save the logTime1.setEnabled(false); so that on rotation or on leaving the activity, it restores it disabled? Eventually I will have another button beside it that will allow you to edit which will unlock the button. Here is the code.
public class TimeLog extends Activity{
boolean logTimeDis1=true;
Button logTime1;
String time1;
TextView ivTimeStamp;
int counter=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.time_log);
logTime1 = (Button) findViewById(R.id.logTime1);
ivTimeStamp = (TextView) findViewById(R.id.ivTimeStamp);
if(savedInstanceState != null) logTime1.setEnabled(savedInstanceState.getBoolean("logTimeDis1", true));
logTime1.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
time1 = new SimpleDateFormat("HH:mm", Locale.US).format(new Date());
ivTimeStamp.setText(time1);
logTime1.setEnabled(false);
logTimeDis1 = false;
return false;
}
});
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
counter++;
}
#Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putString("ivTimeStamp", ivTimeStamp.getText().toString());
outState.putBoolean("logTimeDis1", logTimeDis1);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
ivTimeStamp.setText(savedInstanceState.getString("ivTimeStamp"));
}
}
So, in this line
if(savedInstanceState != null) logTime1.setEnabled(savedInstanceState.getBoolean("logTmeDis1", false));
you don't check if savedInstanceState contains logTmeDis1. Say, savedInstanceState is not null, but does not have logTmeDis1, you'll get false and your button locked. Actually, you don't have to check it in this case, just change false to true, and it should work correctly.
hi i have a menu activity that contains 4 buttons when i press button one the second
activity is open and.i added a back button in second activity to come back to activity
one(menu)how would it perform through code can any one help
private ListView lv;
public static ArrayList<String> your_array_list = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.listview);
lv = (ListView) findViewById(R.id.listView1);
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, your_array_list );
lv.setAdapter(arrayAdapter);
try {
DisplayM.main();
} catch (Exception e) {
e.printStackTrace();
}
// if (ViewClass.theEnd)
// your_array_list.add(ViewClass.methods);
int lst = 0;
for(int i=0; i<lst; i++)
{
}
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
this is my listview activity
Use below code in your back button
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
YourActivity.this.finish();
}
});
Write below code on your activity :-
#Override
public void onBackPressed()
{
// TODO Auto-generated method stub
super.onBackPressed();
}
or
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
or
#Override
public void onBackPressed()
{
// TODO Auto-generated method stub
// if u want to go specific activity then use this code
Intent intent = new Intent(this, YourMainActivity.class);
startActivity(intent);
finish();
}
In your second Activity on your Back Button onClick simply finish() the current (second/third ...) activity. This is what the default back button also does
in pause button you have to just call finish() method and then after finishing current activity it return to parent activity
I am use ads of ad mob for advertise in my application its always display on on start up of my application but I want display this ads on when user exit application and back to the main activity call so how it implement in my app
my code is
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
interstitial = new InterstitialAd(this, "a152eb628a493d8");
adRequest = new AdRequest();
interstitial.loadAd(adRequest);
interstitial.setAdListener(this);
web=(WebView)findViewById(R.id.webview);
web.getSettings().setJavaScriptEnabled(true);
web.setWebViewClient(new WebViewClient());
web.getSettings().setBuiltInZoomControls(true);
web.loadUrl("http://dcs-dof.gujarat.gov.in/live-info.htm");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,1,Menu.NONE,"About");
menu.add(0,2,Menu.NONE,"Feedback");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id=item.getItemId();
if(id == 1)
{
Toast.makeText(MainActivity2.this,"About",Toast.LENGTH_LONG).show();
Intent i=new Intent(MainActivity2.this,about.class);
startActivity(i);
}
else {
Toast.makeText(MainActivity2.this,"Feedback",Toast.LENGTH_LONG).show();
Intent i2 =new Intent(MainActivity2.this,feedback.class);
startActivity(i2);
}
return super.onOptionsItemSelected(item);
}
private boolean doubleBackToExitPressedOnce = false;
#Override
protected void onResume() {
//interstitial.loadAd(adRequest);
super.onResume();
this.doubleBackToExitPressedOnce = false;
}
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this,"Press Again to Exit", Toast.LENGTH_SHORT).show();
}
#Override
public void onDismissScreen(Ad ad) {
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad ad) {
Log.d("OK", "Received ad");
if (ad == interstitial) {
interstitial.show();
}
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
// interstitial.loadAd(adRequest);
super.onRestart();
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
// interstitial.loadAd(adRequest);
super.onStart();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
//interstitial.loadAd(adRequest);
super.onDestroy();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
//interstitial.loadAd(adRequest);
super.onStop();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
// interstitial.loadAd(adRequest);
super.onPause();
}
You really don't want to attempt to display an ad to a user on exit from your Actvity, it is a poor user experience.
Even if you did orchestrate a way to get the ad and present prior to your Activity's destruction you don't know how long it will take to retrieve the ad so your Activity might hang for several seconds on exit. User's hate that kind of thing.
I strongly suggest you find another spot within your app in which to display ads.
i writed an application which contains an option button and Option Activity.
In the OptionActivity, you can choose play a music "on" or "off" with togglebutton. and there are some check button also.
until here everything is okey but when you check some box and back to the main menu and back to the Options Activity again, you find that all variables is default value. for example the checked box one is uncheckhed. I know that it is because of everyCall the Options Activity with new Intent but i want to know that starting an activity "without using new Intent"
This is my SPLASH SCREEN
public class MainActivity extends Activity {
/* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baslangic);
Thread thred=new Thread(){
public void run(){
try {
sleep(5000);
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.OPTIONS"));
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.BASLANGIC"));
} catch (InterruptedException e) {
e.printStackTrace();
}finally{
finish();
}
}
};
thred.start();
}
}
This is my MAIN ACTÄ°VTY which contains options button
public class Baslangic extends Activity {
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start=(Button)findViewById(R.id.button1);
Button options=(Button)findViewById(R.id.button2);
Button about=(Button)findViewById(R.id.button3);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.OYUN"));
}
});
options.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
about.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.ABOUT"));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
And This is My options Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
it=(RadioButton)findViewById(R.id.radio0);
ti=(RadioButton)findViewById(R.id.radio1);
final ToggleButton but=(ToggleButton)findViewById(R.id.toggleButton1);
music=MediaPlayer.create(Options.this, R.raw.avril);
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(but.isChecked())
music.start();
else
music.pause();
}
});
it.setChecked(true);
Button menu=(Button)findViewById(R.id.button1);
menu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.BASLANGIC"));
}
});
}
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
startActivity(new Intent("com.KAYA.ingilizce_gelistirme.BASLANGIC"));
}
#Override
protected void onPause() {
super.onPause();
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onStop() {
super.onStop();
}
}
NOTE: Be aware That when the user click the options Button, i call finish(), actually i have a solution. My solution that, after the splash screen, i start 2 activity; first Options later Menu, And when the user click the Options button and i call the finish() so i am in the options Button :D and when press back i called new Intent(menu) you can see on OPTIONS activity,
So my problem is already solved but i want to know that there should be a solution different from this way, If you know pls help me. :)
Are you using the PreferenceActivity or a normal Activity superclass?
Anyway, be sure to read about the SharedPreferences - it can store your values for entire app or for single activity.
I'm using the TextToSpeech API in my code and it doesn't work when I try to call .speak() function from OnStart(), however it works when I call it from a button onClickListener(). Any idea why? Thank you.
public class TtsDemoActivity extends Activity {
private TextToSpeech mTts;
private OnClickListener buttonListener = new View.OnClickListener() {
#Override
public void onClick(View arg0) {
PlaySound();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ttsdemo);
// Initialize Text To speech
mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int arg0) {
// TODO Auto-generated method stub
}
});
Button myButton = (Button)findViewById(R.id.buttontts1);
myButton.setOnClickListener(buttonListener);
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
PlaySound();
}
protected void PlaySound()
{
String word = "Hello world";
mTts.speak(word, TextToSpeech.QUEUE_FLUSH, null);
}
You must wait until the TTS subsystem signals that it is ready: if it isn't ready when onStart is called, it will fail. If you are trying to speak as soon as it is ready call PlaySound from inside the OnInitListener:
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ttsdemo);
// Initialize Text To speech
mTts = new TextToSpeech(this, new TextToSpeech.OnInitListener() {
#Override
public void onInit(int arg0) {
if(arg0 == TextToSpeech.SUCCESS) PlaySound();
}
});
Button myButton = (Button)findViewById(R.id.buttontts1);
myButton.setOnClickListener(buttonListener);
}
Maybe I am blind, but in your onStart method, you aren't ever calling
PlaySound()
Like you are in
#Override
public void onClick(View arg0) {
PlaySound();
}