Tuesday, 6 June 2017

How To Convert KML File to GeoJson ?

If you want to convert KML file to GeoJson file, refer below mention library for such conversion
  • library Name:osmbonuspack
Example:
In following example we are going use KML file from row folder and convert it in to GeoJson file and sore in SD Card.

 InputStream ins = getResources().openRawResource(  
 getResources().getIdentifier("pmc_election_ward_boundaries",  
 "raw", getPackageName()));  //take file from row folder
 kmlDocument = new KmlDocument();  
 kmlDocument.parseKMLStream(ins, null);  
 fCreated = kmlDocument.saveAsGeoJSON(new File(Environment.getExternalStorageDirectory() + "/sample.geojson"));  

Download Sample Code

Screenshot:
  •  file name sample.geojson is converted from KML file to geoJosn 
  •  GeoJson file plot on mapbox

Upgrade existing Database

When we want to add some new field in existing data base and Don't want to lose old data of user? What we have to do in such situation?
Solution: - While giving updates to android application , some time we are making some changes in database such as adding a new field but the main problem is that our application is live,so we need to take care of user's existing data which is stored in databaseSQLite provides onUpgrade() method to overcome such situation. onUpgrade() is called when database version is changed which means that your database table structure having some changes. Just follow the following steps
  • To add a new column into the existing table, fire "Alter" query in onUpgrade() method of SQLiteOpenHelper 
  • Change version of database(you can not downgrade version of database)
  • 1
    2
    3
    4
    5
    6
    7
    8
    public class DBManager extends SQLiteOpenHelper {
     private SQLiteDatabase sqLiteDatabase;
     private int dbVersion = 2; //changed version
    
            private DBManager(Context context) {
                super(context,"DbSample", null, dbVersion);    
     }
    }
      Remember one thing  onUpgrade() method is called only when version of database  is changed
  • Now write alter query in onUpgrade()
  • 1
    2
    3
    4
    5
    6
     @Override
        public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
            if (newVersion > oldVersion) {
                sqLiteDatabase.execSQL("ALTER TABLE " + tbName + " ADD  COLUMN roll INTEGER DEFAULT 0");
            }
        }

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...