android


How to handle powerbutton in android


here i need to pause music when i click power,home,back buttons, i tried using below code..that code is working for both home and back buttons..but after clicking power button that time sound is still playing...can any one suggest me..
public class MainActivity extends Activity {
Button b1;
MediaPlayer bgmusic;
int length11;
public static final String PREFS_NAME = "MyPrefsFile";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bgmusic=MediaPlayer.create(MainActivity.this,R.raw.bgmusic);
bgmusic.setLooping(true);
bgmusic.start();
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i=new Intent(MainActivity.this,Activityyy2.class);
startActivity(i);
}
});
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
Log.i("-----------onPause----------", "");
super.onPause();
if (bgmusic != null) {
bgmusic.pause();
length11=bgmusic.getCurrentPosition();
}
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
Log.i("-----------onDestroy----------", "");
if (bgmusic != null) {
if (bgmusic.isPlaying()) {
bgmusic.stop();
}
bgmusic.release();
bgmusic = null;
}
super.onDestroy();
}
#Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.i("-----------onRestart----------", "");
}
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
int valuesnd=settings.getInt("silentMode1", 0);
Log.i("-----------onResume----------", "");
Log.i("onResume=-----------",""+valuesnd);
if(valuesnd==2)
{
bgmusic=MediaPlayer.create(MainActivity.this,R.raw.bgmusic);
bgmusic.seekTo(length11);
bgmusic.start();
bgmusic.setLooping(true);
}
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
System.out.println("KEYCODE_HOME");
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", true);
editor.putInt("silentMode1", 2);
editor.commit();
Intent intent = new Intent(
Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
return true;
}
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
System.out.println("KEYCODE_BACK");
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", true);
editor.putInt("silentMode1", 3);
editor.commit();
return true;
}
if (event.getKeyCode() == KeyEvent.KEYCODE_POWER) {
// do what you want with the power button
System.out.println("KEYCODE_POWER");
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", true);
editor.putInt("silentMode1", 4);
editor.commit();
return true;
}
return false;
}
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
}

Related Links

How to pass this type of Curl request in Android Studio
How to send data from activity to connectedThread after moving bluetooth activity into service
push notification with cordova and firebase Cloud messaging:registrationID keep changing
Cordova Android app deploying to Android N device
Android app links clear openning
Real World Use case of RxJava Subject
Communication between multipe fragments
Android - Implementing search filter to a RecyclerView
Cocos2d-x jni How do I connect a Bluetooth keyboard
Stream audio (only) from ffmpeg to Android application
How to always show title in BottomNavigationView [duplicate]
Unable to save image from the directory using FileProvider
Sqlite 3.8 with pre-Lollipop APIs
IconTabBar loses content only on device - cordova android
Get PDF stream from (Xamarin)Android.Print.PrintDocumentAdapter
how to compare my contact with the contacts uploaded in firebase database

Categories

HOME
eclipse
time-complexity
ecmascript-6
twig
numbers
apiblueprint
jsf-2.2
dojo
3d-reconstruction
hpc
settings
accessibility
google-chrome-app
mod-security
enterprise-architect
docx
intershop
android-7.0-nougat
nodemcu
delphi-xe5
user-experience
jquery-callback
direct3d11
roundup
itunes
google-cloud-print
beautifier
appirater
outliers
nstimer
linked-data
angular-translate
errbot
jaws
carrierwave
deltaspike
hreflang
hard-drive
android-maps-extensions
getpixel
openalpr
ods
cin
google-data-api
mongoose-schema
mcustomscrollbar
portforwarding
procfile
proget
x-ray
android-testing
pdf-conversion
pocketsphinx-android
donations
change-tracking
vulcanize
tinkerpop3
macaulay2
rspec2
freepbx
lua-telegram-bot
sklearn-pandas
jparsec
atan2
dynatree
gocql
coda
mod-proxy
zscript
jsvc
pyobjc
actionfilterattribute
java-security
openbabel
lnk
accesscontrolexception
ie8-compatibility-mode
jeromq
actiondispatch
swfobject
sqlclr
pdfviewer
mkv
jsr286
cgimage
commerceserver2007
metaweblog
cfwindow
tui
sifr3
fault-tolerance
information-management
wcf-proxy

Resources

Encrypt Message