Skip to content

Android Blog

Android Blog is the place where I put code possible to be reused in near future.

  • Home
  • example

Tag: example

Example how to take photo and upload it’s thumbnail to your server using okhttp3 (first I convert it to jpeg)

Posted on 17/05/201604/12/2018 By sorsare No Comments on Example how to take photo and upload it’s thumbnail to your server using okhttp3 (first I convert it to jpeg)
Android

//part when you click on item and start camera imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Context context = v.getContext(); if (takePictureIntent.resolveActivity(context.getPackageManager()) != null) { startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE); } } }); //part when you get the thumbnail from camera @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { … Read More “Example how to take photo and upload it’s thumbnail to your server using okhttp3 (first I convert it to jpeg)” »

Wireshark – filter out only results if they are method type POST or GET and not containing in url special word

Posted on 11/06/201504/12/2018 By sorsare No Comments on Wireshark – filter out only results if they are method type POST or GET and not containing in url special word
Non-Android

Example of filtering only that results which have request method as GET or POST and special word is not in url: (http.request.method == GET or http.request.method == POST) and !http.request.uri contains “css”

Notepad++ with regex, re-using match variables

Posted on 11/06/201504/12/2018 By sorsare No Comments on Notepad++ with regex, re-using match variables
Non-Android

Here is an simple example how to use regex in notepad++ with reusing selected match variables. For example we have special code for simulating gcm notifications. We would like to see it in pretty style. To achieve this we must to move all elements with minus to the new lines. How to do it? Here … Read More “Notepad++ with regex, re-using match variables” »

Working example POST in android

Posted on 02/02/201504/12/2018 By sorsare No Comments on Working example POST in android
Android

Here is another short (IMHO) example how to post in android app.

[java]
private void postSms(String text) {
URL url;
HttpURLConnection urlConnection = null;

try {
url = new URL(“http://yourserver.com/sendsms”);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod(“POST”);
urlConnection.setDoOutput(true);

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair(“sms”, text));//POST param as “sms”

OutputStream os = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, “UTF-8”));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();

String status = new String(Streams.getBytes(urlConnection.getInputStream()));
Log.d(TAG, new Exception().getStackTrace()[0].getMethodName() + “:insert status=” + status);

} catch (MalformedURLException | ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
[/java]

This is a method for concatenating parameters for POST

Read More “Working example POST in android” »

GCM notification with custom sound – shorter version (part)

Posted on 26/01/201504/12/2018 By sorsare No Comments on GCM notification with custom sound – shorter version (part)
Android

Here is a short example how to create notification with default stuff but different sound: [java] NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.drawable.ic_launcher) .setContentTitle(“title”) .setStyle(new NotificationCompat.BigTextStyle() .bigText(“message”)) .setContentText(“message”) .setAutoCancel(true) .setSound(soundUri) .setDefaults(NotificationCompat.DEFAULT_ALL ^ NotificationCompat.DEFAULT_SOUND);[/java] The most important part is here: .setDefaults(NotificationCompat.DEFAULT_ALL ^ NotificationCompat.DEFAULT_SOUND) Symbol ^ says “except”, so the last part would say “give me default notification … Read More “GCM notification with custom sound – shorter version (part)” »

Working example – changing drawable background in runtime, dynamically

Posted on 22/01/201504/12/2018 By sorsare No Comments on Working example – changing drawable background in runtime, dynamically
Non-Android

Here is a short example code how to do it:
I’ve created xml file shape.xml and one method for flexible changing color of shape.

shape.xml:
[java]
<?xml version=”1.0″ encoding=”utf-8″?>
<shape xmlns:android=”http://schemas.android.com/apk/res/android” >
<solid android:color=”@android:color/holo_blue_bright” />
<corners android:radius=”30dp” />
<size
android:height=”15dp”
android:width=”15dp” />
</shape>
[/java]

And here is a method for flexible changing color:

Read More “Working example – changing drawable background in runtime, dynamically” »

Working example of setting alarm with repeating stuff, checking if alarm was set with pendingIntent

Posted on 21/01/201504/12/2018 By sorsare No Comments on Working example of setting alarm with repeating stuff, checking if alarm was set with pendingIntent
Android

Here is short and quite (imho) understanding part of code:

[java]//starting #1
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(getActivity(), MyReceiver.class);
intent.setAction(MyReceiver.ACTION_ALARM_RECEIVER);//my custom string action name
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);//used unique ID as 1001
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), aroundInterval, pendingIntent);//first start will start asap[/java]

#1 – first we are getting alarm service. We create own intention and setting own action to it to be able handle in onReceive(), in our receiver class. Then the intention is putting into pendingIntent. The complete pendingIntent is used by alarmManager for a start.
I used System.currentTimeMillis() because I wanted start repeating asap.
I used flag PendingIntent.FLAG_CANCEL_CURRENT for pendingIntent but it might be also PendingIntent.FLAG_UPDATE_CURRENT. It is important to use the same flag everywhere, but when you want stop it, you use PendingIntent.FLAG_NO_CREATE. I will write about it later.
Number 1001 in pendingIntent was used just to be know that our intention has unique ID.

Read More “Working example of setting alarm with repeating stuff, checking if alarm was set with pendingIntent” »

Custom checkable list item with checkbox (layout+code)

Posted on 07/01/201504/12/2018 By sorsare No Comments on Custom checkable list item with checkbox (layout+code)
Android

Here is simple example how to create simple checkable list item with checbox. This a code for widget: [java] public class CheckableTextView extends LinearLayout { private TextView checkLabel; private CheckBox checkBox; public CheckableTextView(Context context) { super(context); } public CheckableTextView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CheckableTextView(Context context, AttributeSet attrs, int defStyleAttr) { … Read More “Custom checkable list item with checkbox (layout+code)” »

  • Android
  • Android UX
  • Non-Android
  • Uncategorized

Tags

activity adb android apk app bat batch change color command connect convert custom database drawable emulator example ffmpeg file filter format gcm gradle greasemonkey install java javascript linux lowercase mac post preview remove rename screen script shape shell solution style time tools windows working xml

Archives

  • August 2023
  • May 2023
  • March 2023
  • December 2022
  • September 2022
  • June 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • May 2021
  • March 2021
  • December 2020
  • March 2020
  • January 2020
  • July 2019
  • June 2019
  • April 2019
  • March 2019
  • February 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • June 2018
  • May 2018
  • April 2018
  • February 2018
  • January 2018
  • December 2017
  • August 2017
  • July 2017
  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • August 2016
  • June 2016
  • May 2016
  • February 2016
  • January 2016
  • September 2015
  • July 2015
  • June 2015
  • April 2015
  • March 2015
  • February 2015
  • January 2015
  • December 2014

Copyright © 2023 Android Blog.

Theme: Oceanly News Dark by ScriptsTown