Skip to content

Instantly share code, notes, and snippets.

@csdear
Created March 2, 2014 17:59
Show Gist options
  • Save csdear/9310696 to your computer and use it in GitHub Desktop.
Save csdear/9310696 to your computer and use it in GitHub Desktop.
Notifications : Status Bar Notification Basic
<<package name>>
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
public class NotificationStatusBarWithCustomViewActivity extends Activity {
//1. Define fields
// Notification ID to allow for future updates
private static final int MY_NOTIFICATION_ID = 1;
// Notification Count
private int mNotificationCount;
// Notification Text Elements
private final CharSequence tickerText = "This is ticker Text";
private final CharSequence contentTitle = "Notification Title";
private final CharSequence contentText = "Notified!";
// Notification Action Elements
private Intent mNotificationIntent;
private PendingIntent mContentIntent;
// Notification Sound and Vibration on Arrival
private Uri soundURI = Uri
.parse("android.resource://course.examples.Notification.StatusBar/"
+ R.raw.alarm_rooster);
private long[] mVibratePattern = { 0, 200, 200, 300 };
RemoteViews mContentView = new RemoteViews(
"course.examples.Notification.StatusBarWithCustomView",
R.layout.custom_notification);
//End Fields
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//2. Define Intents
mNotificationIntent = new Intent(getApplicationContext(),
NotificationSubActivity.class);
mContentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//3. Define the Notification's expanded message(drawer view) and Intent:
mContentView.setTextViewText(R.id.text, contentText + " ("
+ ++mNotificationCount + ")");
//4. Build the Notification
Notification.Builder notificationBuilder = new Notification.Builder(
getApplicationContext())
.setTicker(tickerText)
.setSmallIcon(android.R.drawable.stat_sys_warning)
.setAutoCancel(true)
.setContentIntent(mContentIntent)
.setSound(soundURI)
.setVibrate(mVibratePattern)
.setContent(mContentView);
//5. Pass the Notification to the NotificationManager:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MY_NOTIFICATION_ID,
notificationBuilder.build());
}
});
}
}
=============================================
//SubActivity Intent points to : NotificationSubActivity.java
package course.examples.Notification.StatusBarWithCustomView;
import android.app.Activity;
import android.os.Bundle;
public class NotificationSubActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sub_activity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment