public class NotificationUtils {
private static final String TAG = NotificationUtils. class .getSimpleName();
private static NotificationUtils instance;
private static Context context;
private NotificationManager manager; // ,
private int lastId = 0; // ,
private HashMap<Integer, Notification> notifications; // -
// Singleton
private NotificationUtils(Context context){
this .context = context;
manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notifications = new HashMap<Integer, Notification>();
}
/**
*
*/
public static NotificationUtils getInstance(Context context){
if(instance== null ){
instance = new NotificationUtils(context);
} else{
instance.context = context;
}
return instance;
}
* This source code was highlighted with Source Code Highlighter .
public int createInfoNotification( String message){
Intent notificationIntent = new Intent(context, HomeActivity. class ); // HomeActivity
NotificationCompat.Builder nb = new NotificationCompat.Builder(context)
//NotificationCompat.Builder nb = new NotificationBuilder(context) // Android > 3.0
.setSmallIcon(R.drawable.ic_action_picture) //
.setAutoCancel( true ) //
.setTicker(message) //, -
.setContentText(message) //
.setContentIntent(PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT))
.setWhen(System.currentTimeMillis()) //
.setContentTitle( "AppName" ) //
.setDefaults(Notification.DEFAULT_ALL); // ,
Notification notification = nb.getNotification(); //
manager.notify(lastId, notification); // .
notifications.put(lastId, notification); // id
return lastId++;
}
* This source code was highlighted with Source Code Highlighter .
/**
*
* @param fileName - , .
*/
public int createDownloadNotification( String fileName){
String text = context.getString(R. string .notification_downloading).concat( " " ).concat(fileName); //
RemoteViews contentView = createProgressNotification(text, context.getString(R. string .notification_downloading)); //View
contentView.setImageViewResource(R.id.notification_download_layout_image, R.drawable.ic_stat_example); //
return lastId++; // id,
}
/**
* ProgressBar,
*
* @param text
* @param topMessage , -
* @return View .
*/
private RemoteViews createProgressNotification( String text, String topMessage) {
Notification notification = new Notification(R.drawable.ic_stat_example, topMessage, System.currentTimeMillis());
RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.notification_download_layout);
contentView.setProgressBar(R.id.notification_download_layout_progressbar, 100, 0, false );
contentView.setTextViewText(R.id.notification_download_layout_title, text);
notification.contentView = contentView;
notification.flags = Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_ONLY_ALERT_ONCE;
Intent notificationIntent = new Intent(context, NotificationUtils. class );
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
manager.notify(lastId, notification);
notifications.put(lastId, notification);
return contentView;
}
<? xml version ="1.0" encoding ="utf-8" ? >
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height ="65sp"
android:padding ="10dp"
android:orientation ="vertical" >
< LinearLayout
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:orientation ="horizontal" >
< ImageView
android:id ="@+id/notification_download_layout_image"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:src ="@drawable/ic_stat_example"
android:layout_gravity ="center_vertical" />
< TextView
android:id ="@+id/notification_download_layout_title"
style ="@style/NotificationTitle"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:layout_alignParentTop ="true"
android:layout_marginLeft ="10dip"
android:singleLine ="true"
android:text ="notification_download_layout_title"
android:layout_gravity ="center_vertical" />
</ LinearLayout >
< ProgressBar
android:id ="@+id/notification_download_layout_progressbar"
style ="?android:attr/progressBarStyleHorizontal"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:layout_marginTop ="4dp"
android:progress ="0" />
</ LinearLayout >
<? xml version ="1.0" encoding ="utf-8" ? >
< resources >
< style name ="NotificationText" >
< item name ="android:textColor" > ?android:attr/textColorPrimary </ item >
</ style >
< style name ="NotificationTitle" >
< item name ="android:textColor" > ?android:attr/textColorPrimary </ item >
< item name ="android:textStyle" > bold </ item >
</ style >
</ resources >
<? xml version ="1.0" encoding ="utf-8" ? >
< resources >
< style name ="NotificationText" parent ="android:TextAppearance.StatusBar.EventContent" />
< style name ="NotificationTitle" parent ="android:TextAppearance.StatusBar.EventContent.Title" />
</ resources >
NotificationUtils n = NotificationUtils.getInstance(getActivity());
n.createInfoNotification( "info notification" );
int pbId = NotificationUtils.getInstance(getActivity()).createDownloadNotification( "downloading video" );
NotificationUtils.getInstance(getActivity()).updateProgress(pbId, YOUR_PROGRESS);
android:launchMode="singleTop"
Source: https://habr.com/ru/post/140928/
All Articles