Skip to content

Instantly share code, notes, and snippets.

@rameshvoltella
Forked from csdear/StatusBar
Created November 5, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rameshvoltella/5ab977ad6d26e5a5e8c5 to your computer and use it in GitHub Desktop.
Save rameshvoltella/5ab977ad6d26e5a5e8c5 to your computer and use it in GitHub Desktop.
Notifications : StatusBar With CustomView
package course.examples.Notification.StatusBarWithCustomView;
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 {
//fields
//1. Create ID for notificaiton to allow for future updates
private static final int MY_NOTIFICATION_ID = 1;
//2. Create Notification Counter field
private int mNotificationCount;
//3. Create Notification Text Elements
private final CharSequence tickerText = "tickerText!";
private final CharSequence contentTitle = "Notification";
private final CharSequence contentText = "Notified!";
//4. Create Notification Action Elements for Intents
private Intent mNotificationIntent;
private PendingIntent mContentIntent;
//5. Create media for 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);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//6. Build the Intents
mNotificationIntent = new Intent(getApplicationContext(),
NotificationSubActivity.class);
mContentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
//7. Build the button and on Click listener
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//8. Define the Notification's expanded message and Intent:
mContentView.setTextViewText(R.id.text, contentText + " ("
+ ++mNotificationCount + ")");
//9. Use notificaiton Builder to 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);
//10. Pass the Notification to the NotificationManager:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MY_NOTIFICATION_ID,
notificationBuilder.build());
}
});
}
}
=============================================
//Custom layout view custom_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#7777"
android:padding="3dp" >
<ImageView
android:id="@+id/image"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginRight="10dp"
android:contentDescription="Eye"
android:src="@drawable/fire_eye_alien" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:textSize="24sp" />
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment