Connecting Nox with Android Studio

More info here: https://www.bignox.com/blog/how-to-connect-android-studio-with-nox-app-player-for-android-development-and-debug/

However If You created emulator from nox multiplayer (You can create android 5.1), make sure You have created shortcut to this device and runned by this shortcut.

The rest info related with enabling usb debug and setting to be executed with root permissions is the same.

Raw service with bindings

public class Request2Service extends Service {

    public static final int MSG_REGISTER_WATCHER = -1;
    public static final int MSG_UNREGISTER_WATCHER = -2;

    private ArrayList<Messenger> clients = new ArrayList<>();

    private Messenger clientMessenger = new Messenger(new Handler() {

        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_REGISTER_WATCHER:
                    if (!clients.contains(msg.replyTo)) {
                        clients.add(msg.replyTo);
                    }
                    break;

                case MSG_UNREGISTER_WATCHER:
                    if (clients.contains(msg.replyTo)) {
                        clients.remove(msg.replyTo);
                    }
                    break;

                // TODO: 2017-07-12 case: ... 
            }
        }
    });

    private ExecutorService executor = Executors.newCachedThreadPool();

    private static void sendToClients(ArrayList<Messenger> clients, Message msg) {

        for (int i = clients.size() - 1; i >= 0; i--) {
            try {
                Message msg2 = Message.obtain();
                msg2.what = msg.what;
                msg2.arg1 = msg.arg1;
                msg2.arg2 = msg.arg2;
                msg2.setData(msg.getData());
                msg2.replyTo = msg.replyTo;
                clients.get(i).send(msg2);
            } catch (RemoteException e) {
                clients.remove(i);
            }
        }
    }

    @Override
    public int onStartCommand(final Intent intent, int flags, final int startId) {
        if (intent != null && intent.getAction() != null) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    handleStartCommand(intent);
                }
            });
        }
        return START_STICKY;
    }

    private void handleStartCommand(Intent intent) {
        // TODO: 2017-07-12  
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return clientMessenger.getBinder();
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

 

Passing bitmap to bundle

I found this somewhere in Internet and I thought it would be useful someday

To pass bitmap to bundle, first we ought to convert it to byte array. Then the rest goes straight.

public static byte[] toByteArray(Bitmap bmp) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    return stream.toByteArray();
}

public static Bitmap fromByteArray(byte[] byteArray) {
    return BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
}

 

Helper to compare i.e two months in different years

This method will help in comparing two dates by trimming date format to our specific format. So we avoid unnecessary data.

Sample:

We compare two months in different years. First date is:

2017-10-10 10:00

First we convert it to milliseconds using `SimpleDateFormat`.

We want know only time for

2017-10

So after we get millisecond, we can convert it to another one `yyyy-MM-dd` using code below:

public long getTimeInMillis(long milliSeconds, String dateFormat) {
    SimpleDateFormat formatter = new SimpleDateFormat(dateFormat, Locale.forLanguageTag("pl"));
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(milliSeconds);
    String format = formatter.format(calendar.getTime());

    long milli = milliSeconds;

    SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.forLanguageTag("pl"));
    try {
        Date parse = sdf.parse(format);
        Calendar c = Calendar.getInstance();
        c.setTime(parse);
        milli = c.getTimeInMillis();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    return milli;
}

Utils class: compression string gzip

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Compression {
    public static byte[] compress(String paramString) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(paramString.length());
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        gzipOutputStream.write(paramString.getBytes());
        gzipOutputStream.close();
        byte[] bytes = byteArrayOutputStream.toByteArray();
        byteArrayOutputStream.close();
        return bytes;
    }

    public static String decompress(byte[] paramArrayOfByte) throws IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);
        GZIPInputStream gzipInputStream = new GZIPInputStream(byteArrayInputStream, 32);
        StringBuilder stringBuilder = new StringBuilder();
        byte[] bytes = new byte[32];
        for (; ; ) {
            int i = gzipInputStream.read(bytes);
            if (i == -1) {
                break;
            }
            stringBuilder.append(new String(bytes, 0, i));
        }
        gzipInputStream.close();
        byteArrayInputStream.close();
        return stringBuilder.toString();
    }
}

 

IntentService : has no zero argument constructor

Caused by: java.lang.InstantiationException: java.lang.Class<com.example.MyWakefulService> has no zero argument constructor

What does it means? We created custom IntentService and what? Do we have empty constructor? Nope, so let’s do it!

public class MyWakefulService extends IntentService {

    private static final String TAG = "MyWakefulService";

    public MyWakefulService() {
        super(TAG);
    }
}

Should be OK now.

Reading plain text file in java and android asset

//read lines from file

ArrayList<String> lines = new ArrayList<String>();
try {
    BufferedReader input = new BufferedReader(new FileReader(f));

    String line;
    do {
        if ((line = input.readLine()) != null)
            lines.add(line);
    } while (line != null);
} catch (IOException e) {
    e.printStackTrace();
}
return lines;

//read lines from file asset

StringBuilder sb = new StringBuilder();
try {
    BufferedReader input = new BufferedReader(
            new InputStreamReader(assets.open(fileName)));

    String line;
    do {
        if ((line = input.readLine()) != null)
            sb.append(line);
    } while (line != null);
} catch (IOException e) {
    e.printStackTrace();
}
return sb.toString();