Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Monday, 20 January 2014

Disable Screen Lock in Android


Add this code into your Activity  : 

KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
 KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE); lock.disableKeyguard();


In androidmanifest add this permission: 

<uses-permission android:name="android.permission.DISABLE_KEYGUARD"></uses-permission>

Tuesday, 29 October 2013

Object Serialization

Worrying about loosing object states? Let's do the recovery.....

[Recover and retrieve previous states of objects using Object Serialization]

First see the basic of serialization. You can see and understand what Serialization is. Also in android sometimes there's a need of save the whole state of the object especially when you are making games. Here is the demo project developed by me. https://github.com/jatinmalwal/demo-object-serialization. You'll find it very easy to understand this project and implement it into your application. Whole project is properly documented. The basic implementation of object serialization is similar as Java.

This image properly describes the Java Object Serialization.

Tuesday, 17 September 2013

Are you playing with sounds in your app?

Here is the playground for u.....




If you developing an app and struggling with managing sounds here is the solution;

SoundPoolDemo
Some code snippet :

public class LauncherActivity extends Activity implements OnTouchListener {
private SoundPool soundPool;
final static int MAX_STREAMS = 5;
private boolean isLoaded = false;
private int soundOneID;
private int soundTwoID;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
((RelativeLayout) findViewById(R.id.bg)).setOnTouchListener(this);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
soundPool = new SoundPool(MAX_STREAMS, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {

@Override
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
Log.d("TESTING", "sampleId :" + sampleId + " status :" + status);
isLoaded = true;
}

});

soundOneID = soundPool.load(this, R.raw.shoot_1, 1);
soundTwoID = soundPool.load(this, R.raw.shoot_2, 1);

Log.d("TESTING", "soundOneID :" + soundOneID);
Log.d("TESTING", "soundTwoID :" + soundTwoID);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.launcher, menu);
return true;
}

@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float actualVolume = (float) audioManager
.getStreamVolume(AudioManager.STREAM_MUSIC);
float maxVolume = (float) audioManager
.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = actualVolume / maxVolume;
Log.d("TESTING", actualVolume + ":  actualVolume");
Log.d("TESTING", maxVolume + ":  maxVolume");
Log.d("TESTING", volume + ": volume");
if (isLoaded) {
soundPool.play(soundOneID, volume, volume, 1, 0, 1f);
}
}
return false;
}

@Override
protected void onStop() {
if (soundPool != null) {
// To release all the resources of soundPool
soundPool.release();
}
super.onStop();
}
}

If you have any query, ask me.