Android: detecting the current caller

@Override
public void onReceive(Context context, Intent intent) {

    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    PhoneCallStateListener customPhoneListener = new PhoneCallStateListener(context);
    telephony.listen(customPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    helper = new ContactDatabaseHelper(context);
    list = helper.getAllContacts();

    try{
        incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

        if (list.size() != 0){
            for ( int i  = 0, size = list.size(); i < size; i++ ){
                if (PhoneNumberUtils.compare(incomingNumber, list.get(i).getContactNumber())){                  
                    ToastMsg.showToast(context,list.get(i).getContactName()+" Calling");
                }
            }
        }


    }catch (Exception e) {
        // TODO: handle exception
    }   

}


public class PhoneCallStateListener extends PhoneStateListener{
private Context context;

public PhoneCallStateListener(Context context){
    this.context = context;
}

@Override
public void onCallStateChanged(int state, String incomingNumber) {  

    switch (state) {

        case TelephonyManager.CALL_STATE_RINGING:       


            break;
        case PhoneStateListener.LISTEN_CALL_STATE:

    }
    super.onCallStateChanged(state, incomingNumber);
}
}

Credit Naser Shaikh

Android Studio 1.0 RC2 error: “Gradle DSL method not found: ‘runProguard()'”

After installing the RC2 upgrade (in my case i’ve upgraded directly from 0.8.something to 1.0 rc2) the following error could appear when reimporting/resyncing projects created with previous versions:

Gradle DSL method not found: ‘runProguard()’

Continue reading Android Studio 1.0 RC2 error: “Gradle DSL method not found: ‘runProguard()’”

Enumerating all providers, and respective installed apps, in Android

Android ships with a number of content providers that store common data such as contact information, calendar information, and media files. These classes offer simplified methods of adding or retrieving data from these content providers. If you need to list all the installed providers, for debugging and other purposes, use the following code:
Continue reading Enumerating all providers, and respective installed apps, in Android