Senin, 03 November 2014

Tutorial membuat Aplikasi User Notification


Aplikasi User Notification “Battery Bar”
Aplikasi ini dibuat untuk mengetahui beberapa mode notifikasi yang terdapat pada android. Pada aplikasi ini terdapat tombol push notifikasi dan sebuah text view. Text View berisi daya baterai yang dimiliki smartphone dan tombol push notifikasi untuk memunculkan notification area bar pada smartphone yang berisi notifikasi presentase daya baterai, jika battery kurang dari 20 persen maka akan muncul toast / pemberitahuan untuk charging.
Berikut cara pembuatan aplikasi Battery Bar yang dimulai dari pembuatan baru android application. Pertama – tama kita membuat project android baru dengan nama BatteryBar. Untuk aplikasi ini terdapat logo aplikasi yang nantinya akan menjadi icon aplikasi saat aplikasi terinstall di smartphone. Berikut screenshot untuk mengkonfigurasi inovasi icon aplikasi :

Setelah itu langkah berikutnya kita membuat layout Activity_main.xml yang akan dimunculkan saat awal program dijalankan.
Activity_main.xml
1

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2

xmlns:tools="http://schemas.android.com/tools"
3

android:layout_width="match_parent"
4

android:layout_height="match_parent"
5

android:paddingBottom="@dimen/activity_vertical_margin"
6

android:paddingLeft="@dimen/activity_horizontal_margin"
7

android:paddingRight="@dimen/activity_horizontal_margin"
8

android:paddingTop="@dimen/activity_vertical_margin"
9

tools:context=".MainActivity" >
10


11

<TextView
12

android:id="@+id/batteryTxt"
13

android:layout_width="fill_parent"
14

android:layout_height="wrap_content"
15

android:gravity="center"
16

android:text="@string/hello_world"
17

android:textSize="20sp"/>
18


19

<Button
20

android:id="@+id/notif"
21

android:layout_width="wrap_content"
22

android:layout_height="wrap_content"
23

android:layout_below="@+id/batteryTxt"
24

android:layout_centerHorizontal="true"
25

android:layout_marginTop="96dp"
26

android:text="@string/push" />
27


28

</RelativeLayout>
Berikutnya kita membuat layout dengan nama custom_notification.xml yang digunakan untuk layout notification bar pada smartphone.
custom_notification.xml
1

<?xml version="1.0" encoding="utf-8"?>
2

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3

android:id="@+id/toast"
4

android:layout_width="match_parent"
5

android:layout_height="match_parent"
6

android:background="#7777"
7

android:padding="3dp">
8


9

<ImageView
10

android:id="@+id/img"
11

android:layout_width="44dp"
12

android:layout_height="44dp"
13

android:layout_marginRight="10dp"
14

android:contentDescription="@string/Battery"
15

android:src="@drawable/ic_launcher"
16

/>
17

<TextView
18

android:id="@+id/text"
19

android:layout_width="match_parent"
20

android:layout_height="wrap_content"
21

android:textColor="#FFF"
22

android:textSize="24sp"
23

/>
24


25

</LinearLayout>
                Untuk notifikasi dengan model toast kita juga harus membuat layoutnya, berikut code program dengan nama custom_toast.xml
custom_toast.xml
1

<?xml version="1.0" encoding="utf-8"?>
2

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3

android:layout_width="wrap_content"
4

android:layout_height="wrap_content"
5

android:padding="10dp"
6

android:background="#8181"
7

android:orientation="horizontal" >
8


9

<ImageView
10

android:id="@+id/img"
11

android:layout_width="44dp"
12

android:layout_height="44dp"
13

android:layout_marginRight="10dp"
14

android:contentDescription="@string/Battery"
15

android:src="@drawable/ic_launcher"
16

/>
17

<TextView
18

android:id="@+id/text"
19

android:layout_width="wrap_content"
20

android:layout_height="wrap_content"
21

android:textColor="#FFF"
22

android:text="Charging Your Phone"
23

android:layout_gravity="center_vertical"
24

android:textSize="24sp"
25

/>
26


27

</LinearLayout>
               
                Setelah membuat layout, kemudian membuat kelas MainActivitynya. Berikut source code kelas MainActivity.java :
1

package com.example.batterybar;
2


3

import android.os.BatteryManager;
4

import android.os.Bundle;
5

import android.app.Activity;
6

import android.app.Notification;
7

import android.app.NotificationManager;
8

import android.app.PendingIntent;
9

import android.content.*;
10

import android.view.Gravity;
11

import android.view.Menu;
12

import android.view.View;
13

import android.view.View.OnClickListener;
14

import android.widget.Button;
15

import android.widget.RemoteViews;
16

import android.widget.TextView;
17

import android.widget.Toast;
18

import android.content.Context;
19

import android.content.Intent;
20


21

public class MainActivity extends Activity {
22


23

private static final int MY_NOTIFICATION_ID = 1;
24

//Notif Text Element
25

private final String tickerText = "Your Battery is ";
26

private final CharSequence contentTitle = "Attention";
27

private final String contentText = "This is Notification Bar and
Your Battery is ";
28


29

private Intent mNotificationIntent;
30

private PendingIntent mContentIntent;
31


32

private int level;
33


34

private long[] mVibratePattern = {0, 200, 200, 300};
35

RemoteViews mContentView = new RemoteViews("com.example.statusbar",
36

 R.layout.custom_notification);
37

private TextView contentTxt;
38


39

@Override
40

protected void onCreate(Bundle savedInstanceState) {
41

super.onCreate(savedInstanceState);
42

setContentView(R.layout.activity_main);
43


44

contentTxt = (TextView) this.findViewById(R.id.batteryTxt);
45

BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
46

@Override
47

public void onReceive(Context ctxt, Intent intent) {
48

level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
49

contentTxt.setText(String.valueOf(level) + "%");
50

}
51

};
52

this.registerReceiver(mBatInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
53


54

mNotificationIntent = new Intent(getApplicationContext(), MainActivity.class);
55

mContentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
56

mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
57

final Button button = (Button) findViewById(R.id.notif);
58

button.setOnClickListener(new OnClickListener() {
59

@Override
60

public void onClick(View v) {
61

// TODO Auto-generated method stub
62

mContentView.setTextViewText(R.id.text, contentText + level + "%");
63


64

Notification.Builder notificationBuilder = new Notification.Builder(getApplicationContext())
65

.setTicker(tickerText + level + "%")
66

.setSmallIcon(android.R.drawable.stat_sys_warning)
67

.setAutoCancel(true)
68

.setContentIntent(mContentIntent)
69

.setContentTitle(contentTitle)
70

.setVibrate(mVibratePattern)
71

.setContent(mContentView);
72

NotificationManager mNotificationManager = (NotificationManager) getSystemService(
73

Context.NOTIFICATION_SERVICE);
74

mNotificationManager.notify(MY_NOTIFICATION_ID, notificationBuilder.build());
75

//show toast notification
76

if (level < 20) {
77

Toast toast = new Toast(getApplicationContext());
78

toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
79

toast.setDuration(Toast.LENGTH_LONG);
80

toast.setView(getLayoutInflater().inflate(R.layout.custom_toast, null));
81

toast.show();
82

}
83

}
84

});
85

}
86


87

@Override
88

public void onBackPressed() {
89

// TODO Auto-generated method stub
90

finish();
91

}
92


93

@Override
94

public boolean onCreateOptionsMenu(Menu menu) {
95

// Inflate the menu; this adds items to the action bar if it is present.
96

getMenuInflater().inflate(R.menu.main, menu);
97

return true;
98

}
99


100

}

      Pada android manifest kita sedikit menambahkan beberapa code program untuk edit permission vibrate, karena pada program ini, jika kita menekan tombol push notification, smartphone akan bergetar. Berikut source code pada android manifest.xml :
1

<?xml version="1.0" encoding="utf-8"?>
2

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3

package="com.example.batterybar"
4

android:versionCode="1"
5

android:versionName="1.0">
6


7

<uses-sdk
8

android:minSdkVersion="16"
9

android:targetSdkVersion="18" />
10

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


12

<application
13

android:allowBackup="true"
14

android:icon="@drawable/ic_launcher"
15

android:label="@string/app_name"
16

android:theme="@style/AppTheme" >
17

<activity
18

android:name="com.example.statusbar.MainActivity"
19

android:label="@string/app_name" >
20

<intent-filter>
21

<action android:name="android.intent.action.MAIN" />
22


23

<category android:name="android.intent.category.LAUNCHER" />
24

</intent-filter>
25

</activity>
26

</application>
27


28

</manifest>

Screenshot Program awal dijalankan dan saat push notification ditekan :



Screenshot Program saat daya baterai kurang dari 20 akan muncul toast:



Untuk file installernya dapat didownload di link https://www.dropbox.com/s/xxf1k36omy33w4i/BatteryBar.apk?dl=0

Terima kasih atas kunjungannya :)