Techbirds | Knowledge Hub | Page 7

To use the HttpClient in Windows phone we need theMicrosoft HTTP Client Libraries which we can get fron the nuget easily. following is the code by which we can use the HttpClient

private async Task PerformGet() { HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(myUrlGet); if (response.IsSuccessStatusCode) { // if the response content is a byte array byte[] contentBytes = await response.Content.ReadAsByteArrayAsync(); // if the response content is a stream Stream contentStream = await response.Content.ReadAsStreamAsync(); // if the response content is a string (JSON or XML) string json = await response.Content.ReadAsStringAsync(); // your stuff.. } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

private async Task PerformGet()

{

HttpClient client = new HttpClient();

HttpResponseMessage response = await client.GetAsync(myUrlGet);

if (response.IsSuccessStatusCode)

{

// if the response content is a byte array

byte[] contentBytes = await response.Content.ReadAsByteArrayAsync();

// if the response content is a stream

Stream contentStream = await response.Content.ReadAsStreamAsync();

// if the response content is a string (JSON or XML)

string json = await response.Content.ReadAsStringAsync();

// your stuff..

}

}

reference from the link 948 total views, no views today

948 total views, no views today

Hi Folks, Refer this code for identifying your 2G network enabled or not.

int enabled = Settings.Secure.getInt(getContentResolver(), “preferred_network_mode”, -1); Log.d(“MYAPP”, “2G only enabled: ” + enabled); Where the “use only 2G networks” setting is specified as: 0 indicates the setting is disabled 1 indicates the setting is enabled -1 indicates the setting is not set (some devices?) How I discovered this? I gathered all the key/value pairs from Settings.Secure using the following: ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(Settings.Secure.CONTENT_URI, null, null, null, null); if (cursor.moveToFirst()) { while (!cursor.isAfterLast()) { Log.d(“MYAPP”, “cursor: ” + cursor.getString(0) + “, ” + cursor.getString(1) + “, ” + cursor.getString(2)); cursor.moveToNext(); } }

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

int enabled = Settings.Secure.getInt(getContentResolver(),

        “preferred_network_mode”, -1);

Log.d(“MYAPP”, “2G only enabled: ” + enabled);

Where the “use only 2G networks” setting is specified as:

0 indicates the setting is disabled

1 indicates the setting is enabled

-1 indicates the setting is not set (some devices?)

How I discovered this? I gathered all the key/value pairs from Settings.Secure using the following:

ContentResolver cr = getContentResolver();

Cursor cursor = cr.query(Settings.Secure.CONTENT_URI, null, null, null, null);

if (cursor.moveToFirst()) {

    while (!cursor.isAfterLast()) {

        Log.d(“MYAPP”, “cursor: “

                + cursor.getString(0) + “, “

                + cursor.getString(1) + “, “

                + cursor.getString(2));

        cursor.moveToNext();

    }

}

Kind Regards, Tapan Saini (Android Developer) 623 total views, no views today

623 total views, no views today

Hi Folks, TOday i will explain some common tasks that might be helpful while making android apps: Call An Activity form Reciever

Intent i = new Intent(); i.setClassName(“com.example.gamedoc”, “com.example.gamedoc.OpenAppDialogActivity”); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NEW_TASK ); context.startActivity(i)

Intent i = new Intent();

        i.setClassName(“com.example.gamedoc”,

                “com.example.gamedoc.OpenAppDialogActivity”);

        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK |Intent.FLAG_ACTIVITY_NEW_TASK );

        context.startActivity(i)

  On Boot Completing, Starting a service:

public void startService() { try { if (intent == null) { intent = new Intent(Recieved_COntext, Teri_Service.class); } Calendar cal = Calendar.getInstance(); if (pIntent == null) { pIntent = PendingIntent.getService(mContext, 0, intent, 0); } if (alarms == null) { alarms = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); } alarms.setInexactRepeating(AlarmManager.RTC, cal.getTimeInMillis(), 400, pIntent); } catch (Exception e) { e.printStackTrace(); } }

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

public void startService() {

        try {

            if (intent == null) {

                intent = new Intent(Recieved_COntext, Teri_Service.class);

            }

            Calendar cal = Calendar.getInstance();

            if (pIntent == null) {

                pIntent = PendingIntent.getService(mContext, 0,

                        intent, 0);

            }

            if (alarms == null) {

                alarms = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE);

            }

            alarms.setInexactRepeating(AlarmManager.RTC, cal.getTimeInMillis(),

                    400, pIntent);

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

  Kind Regards, Tapan Saini (Android Developer) 617 total views, no views today

617 total views, no views today

Hi Folks, Today , i will discuss some very common concepts that some developers think is just a theoretical concept and feel proud to have little practical knowledge, but this is very useful if one wants to be a good biggy developer. Semaphores are just variables of integer king that  helps code to provide lock

Read More →

787 total views, no views today

Code to extract the images from canvas and save it into Sd card of mobile in Android :   //canvasview is an object or reference to Canvas { canvasview.setDrawingCacheEnabled(true); canvasview.buildDrawingCache(true); // MainActivity.bitmap2=canvasview.getDrawingCache(true); Bitmap imgData = Bitmap.createBitmap(canvasview .getDrawingCache(true)); DateFormat dateFormat = new SimpleDateFormat(“yyyy/MM/dd HH:mm:ss”); Calendar cal = Calendar.getInstance(); storeImage(imgData, “/” + cal.getTime().toString().replace(‘:’, ‘_’) + “.png”); Toast.makeText(EditorActivity.this,

Read More →

644 total views, no views today