Android development

Developing in Visual Studio

http://ian-ni-lewis.blogspot.nl/2011/01/its-like-coming-home-again.html
https://github.com/gavinpugh/vs-android

Installation Android Studio

- Install Android Studio, preferably with JDK included.
- If JDK not included, install Java
- Enable the developer options on your phone
- In the developer options, enable USB-debugging
- Install the USB-drivers for your phone (E:\Downloads\Drivers\samsung_usb_driver_for_mobile_phones.exe)

Showing messages to the user

Notification

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class NotificationHelper {
    public static void showNotification(Context context, int id, String title, String text) {
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha)
                        .setContentTitle(title)
                        .setContentText(text);

        PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                new Intent(context, MainActivity.class), 0);

        mBuilder.setContentIntent(contentIntent);

        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(id, mBuilder.build());
    }
}

Toast

Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

AndroidManifest.xml

http://developer.android.com/training/basics/firstapp/running-app.html

Contains the activities and permissions.

Permissions

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

Conversion

Int type = Integer.parseInt(typeString);
String type = Integer.toString(typeInteger);
String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

Debugging

General

https://developer.android.com/tools/debugging/debugging-studio.html

Logging

import android.util.Log;
Log.d("finished", "onCreate");
Mind that the application has to be started in Debug mode (Menu Build - Debug).
You can check the log in 5: Debug or in 6: Android

Others

Generate random number

import java.util.Random;

public int GetRandomNumber()
{
  Random randomGenerator = new Random();
  return randomGenerator.nextInt(maximumNumber);
}
will generate a random number from 0 to maximumNumber - 1
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License