Thursday, 27 April 2017

Phone call Blocking in Android

You can block phone call from you activity/app.we can block phone by two way either block call for specific activity or block call for complete device.
  1. Broadcast receiver to handle Incoming calls: 
  2.  following code is just sample code, it will block all incoming calls, at line number 19 you will get the cell phone number of incoming call.use that number and apply your logic to block specific calls
     
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    public class BlockCallReceiver extends BroadcastReceiver {
    
        Context context;
    
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Log.d("Call 1","work");
            Toast.makeText(context," Receiver start - call1 ",Toast.LENGTH_LONG).show();
            Bundle myBundle = intent.getExtras();
            if (myBundle != null) {
                System.out.println("--------Not null-----");
                try {
                    if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
                        String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
                        System.out.println("--------in state-----");
                        if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
                            // Incoming call
                            String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
                            System.out.println("--------------my number---------" + incomingNumber);
    
                        // this is main section of the code,. could also be use for particular number.
                            // Get the boring old TelephonyManager.
                            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    
                            // Get the getITelephony() method
                            Class<?> classTelephony = Class.forName(telephonyManager.getClass().getName());
                            Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");
    
                            // Ignore that the method is supposed to be private
                            methodGetITelephony.setAccessible(true);
    
                            // Invoke getITelephony() to get the ITelephony interface
                            Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);
    
                            // Get the endCall method from ITelephony
                            Class<?> telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
                            Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");
    
                            // Invoke endCall()
                            methodEndCall.invoke(telephonyInterface);
    
                        }
    
                    }
                } catch (Exception ex) { // Many things can go wrong with reflection calls
                    ex.printStackTrace();
                }
            }
        }
    }
    

  3. Block Call for Specific activity: 
    1. when you register the call blocking broadcast receiver to specific activity then your receiver only work for that specific activity when that activity is close then broadcast receiver stop the working 
    2. To register broadcast receiver to Activity just refer following lines code
    3.  
    4.   1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      public class MainActivity extends Activity {
      
          IntentFilter filter = new IntentFilter("android.intent.action.PHONE_STATE");
          BlockCallReceiver blockCallReceiver;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
      
          }
      
          @Override
          protected void onResume() {
              super.onResume();
              blockCallReceiver = new BlockCallReceiver();
              registerReceiver(blockCallReceiver, filter);
          }
      
          @Override
          protected void onDestroy() {
              super.onDestroy();
              unregisterReceiver(blockCallReceiver);
          }
      }
      
  4.  Block Call for Application:
    1.  when you register your receiver in manifest for application then your all call will be blocked even when your application is close .
    2.  In application tag  insert the receiver tag to register broadcast receiver 
      1
      2
      3
      4
      5
      <receiver android:name=".BlockCallReceiver">
                  <intent-filter>
                      <action android:name="android.intent.action.PHONE_STATE"></action>
                  </intent-filter>
              </receiver>
      
Download Complete Sample

Saturday, 22 April 2017

Set Drawable at Run time

If you want change drawableLeft/Right/Top/Bottom property of view at run time then use following method
  •  setCompoundDrawablesWithIntrinsicBounds(int left,int top,int right,int bottom)
 Note: When we are setting this property layout padding and drawable padding are reset  so we need to set padding again.
  • setPadding(int left, int top, int right, int bottom) :  Set layout padding
  • setCompoundDrawablePadding: Set drawable padding
Example : In following example we have set drawableLeft property to EditText and DrawableRight property to TextView

     editTextDrawableLeft = (EditText)findViewById(R.id.editText_drawable_left);  
     textViewDrawableRight =(TextView)findViewById(R.id.textView_drawable_right);  
   
     //here we are setting image to left side of editText  
     editTextDrawableLeft.setCompoundDrawablesWithIntrinsicBounds(R.drawable.user_profile,0,0,0);  
     //now set the padding and drawable to editText  
     editTextDrawableLeft.setPadding(10,10,10,10);  
     editTextDrawableLeft.setCompoundDrawablePadding(20);  
   
     //now set the drable to textView  
     textViewDrawableRight.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.user_profile,0);  
     textViewDrawableRight.setPadding(10,10,10,10);  
     textViewDrawableRight.setCompoundDrawablePadding(20)  

Download Complete Example  

Screen Shot :



Create File on External Storage using Storage access framework for android 10

How To Create File on External Storage on android 10 using Storage Access Framework? By default, apps targeting Android 10 and higher a...