(Android)Membuat Natification

Salam olah raga, kok olah raga….

karena kalau kita sebelum bermain dengan code, sebaiknya fresh terlebih dahulu… karena dengan pikiran dan jiwa yang fresh kita semua itu akan berjalan dengan lancar, kali ini masih bermain dengan Robot hijau, kita akan membuat natification sederhana… langsung ke lebar hijau android…

1. Create Project dengan nama Natification

2. Buka file main.xml

disini kita hanya menggunakan actifitas button..

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="createNotification"
        android:text="Create Notification" >
    </Button>

</RelativeLayout>

3. Create File XML dengan nama result.xml

disini kita hanya menggunakan TextView untuk mengambil pesan yang akan di keluarkan..

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the result activity opened from the notification" >
    </TextView>

</LinearLayout> 

4. Create Class dengan nama NatificationReceiver

 package com.example.natification;

import android.app.Activity;
import android.os.Bundle;

public class NotificationReceiver extends Activity{
	@Override
	  protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.result);
	  }
}

5. Buka file main.java

kita disini hanya membuat method untuk mengambil data dari class sebelumnya..

 package com.example.natification;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	 public void createNotification(View view) {
		    NotificationManager notificationManager = (NotificationManager) 
		          getSystemService(NOTIFICATION_SERVICE);
		    Notification notification = new Notification(R.drawable.ic_launcher,
		        "A new notification", System.currentTimeMillis());
		    // Hide the notification after its selected
		    notification.flags |= Notification.FLAG_AUTO_CANCEL;

		    Intent intent = new Intent(this, NotificationReceiver.class);
		    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
		    notification.setLatestEventInfo(this, "This is the title",
		        "This is the text", activity);
		    notification.number += 1;
		    notificationManager.notify(0, notification);

		  }
}

6. Run dan hasilnya..

Capture

Advertisements

Published by

jarcode

hello I am jarcode

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s