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>

Thursday 2 January 2014

Guarded Blocks (Deep into Object)

Do you always wait for your condition to be true using While(!condition) { } ??


Really ?? O_O

But trust me, there is always a "Better Way". Isn't it?

Threads often have to coordinate their actions. The most common coordination idiom is the guarded block. Such a block begins by polling a condition that must be true before the block can proceed.
Suppose, for example guardedJoy is a method that must not proceed until a shared variable joy has been set by another thread. Such a method could, in theory, simply loop until the condition is satisfied, but that loop is wasteful, since it executes continuously while waiting.
public void guardedJoy() {
    // Simple loop guard. Wastes
    // processor time. Don't do this!
    while(!joy) {}
    System.out.println("Joy has been achieved!");
}
A more efficient guard invokes Object.wait to suspend the current thread. The invocation of wait does not return until another thread has issued a notification that some special event may have occurred — though not necessarily the event this thread is waiting for:
public synchronized void guardedJoy() {
    // This guard only loops once for each special event, which may not
    // be the event we're waiting for.
    while(!joy) {
        try {
            wait();
        } catch (InterruptedException e) {}
    }
    System.out.println("Joy and efficiency have been achieved!");
}

Note: Always invoke wait inside a loop that tests for the condition being waited for. Don't assume that the interrupt was for the particular condition you were waiting for, or that the condition is still true.

Like many methods that suspend execution, wait can throw InterruptedException. In this example, we can just ignore that exception — we only care about the value of joy.
Why is this version of guardedJoy synchronized? Suppose d is the object we're using to invoke wait. When a thread invokes d.wait, it must own the intrinsic lock for d — otherwise an error is thrown. Invoking wait inside a synchronized method is a simple way to acquire the intrinsic lock.
When wait is invoked, the thread releases the lock and suspends execution. At some future time, another thread will acquire the same lock and invoke Object.notifyAll, informing all threads waiting on that lock that something important has happened:
public synchronized notifyJoy() {
    joy = true;
    notifyAll();
}
Some time after the second thread has released the lock, the first thread reacquires the lock and resumes by returning from the invocation of wait.

Note: There is a second notification method, notify, which wakes up a single thread. Because notify doesn't allow you to specify the thread that is woken up, it is useful only in massively parallel applications — that is, programs with a large number of threads, all doing similar chores. In such an application, you don't care which thread gets woken up.

Let's use guarded blocks to create a Producer-Consumer application.  For complete documentation follow the link.

http://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html

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.

Sunday 15 September 2013

Introduction

Let me introduce this blog first. This is a blog for all the android developers. As the name describes, it is a method of the interface AndroidDevelopersPoint. Here is the source :

public interface AndroidDevelopersPoint{
       public Android getAndroidInMyStyle(Style yourStyle);
}

You can find here lots of android stuff like links of 
  • Cool libraries by top developers implemented by me
  • Android demo projects developed by me 
  • Various top rated answers given by me on stackoverflow and android.stackexchange
  • Various quality questions with answers asked by me on the same
  • Cool apps developed by me
  • Top google play apps in my collections  

Method description : 

It's a method for all the ADs (Android Developers) to provide their style and get the android in their style. You'll always find solutions to there problems here. It's a interface method so you can always provide implementation to it.