Archive

Archive for March, 2016

Show Notification when Air-plane mode change (Using Broadcast Receiver)

March 31st, 2016 No comments

Hello guys!

Today I have a simple example code that demonstrate the use of Broadcast Receiver in Android, this tutorial also help you to understand how to show a Notification.

Ok now we start this example.

Step 1: Create a new class named “BroadCastRec” which is extended from BroadCastReceiver class:

 

Step 2: Declare this receiver in manifest, notice the action name:

<receiver android:name=”com.example.admin.broadcastreceiver.BroadCastRec”>
<intent-filter>
<action android:name=”android.intent.action.AIRPLANE_MODE”/>
</intent-filter>
</receiver>

 

Step 3: MainActivity.java:

 

 

Now let’s run our program on physical device and then try to change Air-plane mode.

Good luck!

Check if a Database exist on device.

March 26th, 2016 No comments

Sometime you need to check if a database which is exist or not on your phone.

Below is a simple statement for you to do this, a parameter “dbName” is your Database’s name.

private boolean isDatabaseExist(String dbName){
File dbFile = this.getDatabasePath(dbName);
return dbFile.exists();
}

 

Categories: Android, SQLite Tags:

Using your flashLight on android device as a Torch

March 20th, 2016 No comments

2016-03-20_22-47-33.gif

This is a very simple project to demonstrate the usage of your flash light on android device. Which was built on the latest version of android studio (March 20th, 2016).

Notice! Due to the problem when the orientation changes, you should read carefully & focus on android life-cycle to make this better.

This project on Github

Categories: Android Tags: ,

Tìm kiếm nhị phân (ví dụ trên c++)

March 9th, 2016 No comments

Ý tưởng tương tự thuật toán sắp xếp quá nổi tiếng QuickSort.

Sau mỗi bước tìm kiếm thì số phần tử trong lần xét sau chỉ còn 1 nửa. Cho nên thời gian thực thi của thuật tìm kiếm này là O(LogN).

sample:

 

[code lang=cpp]
<br />// mảng a chứa các phần tử đã được sắp xếp (điều kiện cần cho thuật toán này).

// n là số lượng phần tử trong mảng a này.

// x là phần tử cần tìm
int TimKiemNhiPhan(int a[MAX], int left, int right, int mid, int x)
{
if (left > right)return -1;
if (a[mid] == x)return mid;
if (a[mid] > x)return TimKiemNhiPhan(a, left, mid-1, (left+(mid-1))/2, x);
return TimKiemNhiPhan(a, mid + 1, right, (mid + 1 + right) / 2, x);
}

void Result()
{
int res = TimKiemNhiPhan(a, 0, n – 1, (n – 1) / 2, x);
if (vt == -1)
cout << "\nKhong tim thay"<<" "<< x;
else cout << "\nTim thay:"<<" "<<x << " "<<"tai vi tri la:" << vt;
}
[/code]

Bài toán Mã đi tuần (ví dụ trên c++)

March 9th, 2016 No comments

[code lang=cpp]
#include <iostream>
#include <iomanip>

using namespace std;

// Input
int n = 8;
int x, y;

// Output
int BanCo[8][8];

int dx[] = {-2, -1, +1, +2, +2, +1, -1, -2};
int dy[] = {+1, +2, +2, +1, -1, -2, -2, -1};

void Xuat()
{
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
cout << setw(3);
cout << BanCo[i][j] << " ";
}
cout << endl;
}
cout << endl;
}

// Tim 1 trong 8 o xung quanh cua (x,y) de dat Ma
void MaDiTuan(int x, int y, int buoc)
{
if (buoc > n*n)
Xuat();
else
{
// Xet 8 o xung quanh
for (int k=0; k<8; k++)
{
int xx = x + dx[k];
int yy = y + dy[k];

// Nằm trong bang nxn11111111111111111111
if (xx>=0 && yy>=0 && xx<n && yy<n)
if (BanCo[xx][yy]==0) // Chua có Mã
{
BanCo[xx][yy] = buoc;
//Xuat();
MaDiTuan(xx, yy, buoc+1);
BanCo[xx][yy]=0;
}
}
}
}

int main()
{
// Nhap x, y
x = 7;
y = 3;
n=8;

for (int i=0; i<n; i++){
for (int j=0; j<n; j++){
BanCo[i][j]=0;
}
}

int buoc=1;
BanCo[x][y] = buoc;
MaDiTuan(x, y, buoc+1);

return 0;
}
[/code]