[source code] Android Development Tutorial - 61 & 62

+10 Bucky Roberts · January 14, 2015

activity_main.xml
<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" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click for Notification"
        android:id="@+id/buckysButton"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="buckysButtonClicked" />
</RelativeLayout>


MainActivity.java
package com.thenewboston.notification;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends ActionBarActivity {

    NotificationCompat.Builder notification;
    private static final int uniqueID = 45612;

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

        notification = new NotificationCompat.Builder(this);
        notification.setAutoCancel(true);
    }

    public void buckysButtonClicked(View view){
        //Build the notification
        notification.setSmallIcon(R.drawable.ic_launcher);
        notification.setTicker("This is the ticker");
        notification.setWhen(System.currentTimeMillis());
        notification.setContentTitle("Here is the title");
        notification.setContentText("I am the body text of your notification");

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        notification.setContentIntent(pendingIntent);

        //Builds notification and issues it
        NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        nm.notify(uniqueID, notification.build());

    }

}

Post a Reply

Replies

Oldest  Newest  Rating
0 niranga sandaruwan · January 16, 2015
greate tutorial can you explain how to trigger an alarm on  day of week as a example monday only,only weekend,only weekdays
0 Kuroodo Ditory · January 16, 2015
@niranga sandaruwan There should be a clock/calendar already implemented in android. Use it to get the current day, then if it is a specific day (or weekened/weekday), ring the alarm!

Did some research before posting and found this:
http://stackoverflow.com/questions/5574673/what-is-the-easiest-way-to-get-the-current-day-of-the-week-in-android
0 Rip Van Winkle · August 19, 2015
Worked great! Very useful stuff. The only problem I had was finding the little android icon, since it was in mipmap (instead of drawable) in my version of Android Studio. (No idea why.) Anyway, if anyone else has this problem, it's easy to fix. Just change this line
 notification.setSmallIcon(R.drawable.ic_launcher);

to this:
myNotification.setSmallIcon(R.mipmap.ic_launcher);
0 Jack Kuzmov · August 19, 2015
 Hey Bucks, probably wrong page to ask my question , but I did Gradle Setup with android sdk and everything. But now I have


this lil errors in Eclipse now. Can you help me here and say what's wrong?
/images/forum/upload/2015-08-19/867c713a49e404443382d2c787c4d4fd.jpg
0 Aleksandar Jošić · November 12, 2015
This doesn't work. Bucky pls check and update those tutorials. I wasted whole day on latest 20 tutorials from this series because some of them I couldn't catch because you were too fast explaining and writing, some of them you changed or added code in behind without notifying them and some of them, like this one, didn't work at all...
0 niraj kumar · November 15, 2015
i want to run my android application in background mode while i will get a incoming calls
0 Arnab Kundu · April 1, 2016
code for notification sound and vibration please...
  • 1

Java / Android Development

106,984 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator