Posts

Starting an Activity with Toast from IntentService

package in.co.wizardinfosolutions.batch270705; import android.app.IntentService; import android.content.Intent; import android.content.Context; import android.os.Looper; import android.os.Handler; import android.support.annotation. Nullable ; import android.widget.Toast; /** * An { @link IntentService} subclass for handling asynchronous task requests in * a service on a separate handler thread. * <p> * TODO: Customize class - update intent actions, extra parameters and static * helper methods. */ public class MyIntentService extends IntentService { public MyIntentService() { super ( "MyIntentService" ); } @Override protected void onHandleIntent( @Nullable Intent intent) { try {Thread. sleep ( 3000 );} catch (Exception ex){} showToast( "Starting Main Activity" ); Intent ma = new Intent(getBaseContext(), MainActivity. class ); ma.addFlags(Intent. FLAG_ACTIVITY_NEW_TASK ); ...

Creating and using IntentService

package in.co.wizardinfosolutions.servicetest; import android.app.IntentService; import android.content.Intent; import android.os.Handler; import android.os.Looper; import android.support.annotation. Nullable ; import android.widget.Toast; public class MyIntentService extends IntentService { public MyIntentService() { super ( "MyIntentService" ); } @Override protected void onHandleIntent( @Nullable Intent intent) { for ( int i= 1 ;i<= 10 ;i++) { showToast( "Number is " +i); try {Thread. sleep ( 1000 );} catch (Exception ex){} } } protected void showToast( final String msg){ Handler handler = new Handler(Looper. getMainLooper ()); handler.post( new Runnable() { @Override public void run() { Toast. makeText (getApplicationContext(), msg , Toast. LENGTH_SHORT ).show(); } }); } } package in.co.wizardi...

Adding Map into Android App

 String latlong ="28.676111,77.470293";  WebView wv = findViewById(R.id.vwMap);  WebSettings webSettings = wv.getSettings();  webSettings.setJavaScriptEnabled(true);  wv.loadUrl("https://www.google.com/maps/search/?api=1&query=" + latlong); Add the following permission < uses-permission android :name= "android.permission.INTERNET" />

Creating Icon for Android App

Android Asset Studio A collection of tools to easily generate assets such as launcher icons for your Android app. http://romannurik.github.io/AndroidAssetStudio/index.html

Using SQLite with Android

Creating database and its tables import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class MyHelper extends SQLiteOpenHelper { public MyHelper(Context c) { super (c, "databasename" , null , 23 ) ; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL( "CREATE TABLE statement" ); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL( "drop table statement" ); } } Using the controller import android.content.Context; import android.database.Cursor; import android.database.sqlite.*; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; public class Db { SQLiteDatabase db ; SQLiteOpenHelper helper ; public Db(Context c) { helper = new MyHelper(c); db = helper ....

Using JSON Object with PHP

Creating JSON object in PHP Define the key/value pairs to send the data to the client Use json_encode() function to convert the object into JSON format before sending to the client <?php $myObj=(object)[]; $myObj->name = "Tanya Chauhan"; $myObj->age = 20; $myObj->city = "Ghaziabad"; $myJSON = json_encode($myObj); echo $myJSON; ?> To receive this data on the client side using AJAX and show on the web page using JavaScript built-in object JSON with methods JSON.parse() <!doctype html> <html> <head> <script src="jquery-3.2.1.min.js" type="text/javascript"></script> <script>                 $(document).ready(function()                 {                                 $("#b1").click(function()                   ...

Sending data to PHP Server and Receiving the Response

URL url; url = new URL( "any url" ); URLConnection connection; connection = url.openConnection(); HttpURLConnection httppost = (HttpURLConnection) connection; httppost.setDoInput( true ); httppost.setDoOutput( true ); httppost.setRequestMethod( "POST" ); httppost.setRequestProperty( "User-Agent" , "Tranz-Version-t1.914" ); httppost.setRequestProperty( "Accept_Language" , "en-US" ); httppost.setRequestProperty( "Content-Type" , "application/x-www-form-urlencoded" ); String returnvalue; InputStream in = httppost.getInputStream(); StringBuffer sb = new StringBuffer(); try { int chr; while ((chr = in.read()) != - 1 ) { sb.append(( char ) chr); } returnvalue = sb.toString(); } finally { in.close(); }