下面列出了android.content.Context#NOTIFICATION_SERVICE 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void newNotify() {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.update_new;
CharSequence tickerText = "我的通知栏标题";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);// 定义下拉通知栏时要展现的内容信息
Context context = getApplicationContext();
CharSequence contentTitle = "我的通知栏标展开标题";
CharSequence contentText = "我的通知栏展开详细内容";
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
// 用mNotificationManager的notify方法通知用户生成标题栏消息通知
mNotificationManager.notify(1, notification);
}
private void setNotifiction() {
RemoteViews notificationView = new RemoteViews(this.getPackageName(), R.layout.player_notification);
notificationView.setTextViewText(R.id.notificationSongArtist, getArtist());
notificationView.setTextViewText(R.id.notificationSongTitle, getCurrentTitle());
/* 1. Setup Notification Builder */
Notification.Builder builder = new Notification.Builder(this);
/* 2. Configure Notification Alarm */
builder.setSmallIcon(R.drawable.ic_stat_notify).setAutoCancel(true).setWhen(System.currentTimeMillis())
.setTicker(getCurrentTitle());
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent notifIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentIntent(notifIntent);
builder.setContent(notificationView);
/* 4. Create Notification and use Manager to launch it */
Notification notification = builder.build();
String ns = Context.NOTIFICATION_SERVICE;
notifManager = (NotificationManager) getSystemService(ns);
notifManager.notify(NOTIFICATION_ID, notification);
startForeground(NOTIFICATION_ID, notification);
}
/**
* Displays a notification in the notification area of the UI
* @param context Context from which to create the notification
* @param messageString The string to display to the user as a message
* @param intent The intent which will start the activity when the user clicks the notification
* @param notificationTitle The resource reference to the notification title
*/
static void notifcation(Context context, String messageString, Intent intent, int notificationTitle) {
//Get the notification manage which we will use to display the notification
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
long when = System.currentTimeMillis();
//get the notification title from the application's strings.xml file
CharSequence contentTitle = context.getString(notificationTitle);
//the message that will be displayed as the ticker
String ticker = contentTitle + " " + messageString;
//build the pending intent that will start the appropriate activity
PendingIntent pendingIntent = PendingIntent.getActivity(context,
0, intent, 0);
//build the notification
Builder notificationCompat = new Builder(context);
notificationCompat.setAutoCancel(true)
.setContentTitle(contentTitle)
.setContentIntent(pendingIntent)
.setContentText(messageString)
.setTicker(ticker)
.setWhen(when)
.setSmallIcon(R.mipmap.ic_launcher);
Notification notification = notificationCompat.build();
//display the notification
mNotificationManager.notify(MessageID, notification);
MessageID++;
}
public static void showNotification(Context context, Settings settings, String text) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
if (!settings.isShowNotification()) {
// clear existing notification
mNotificationManager.cancel(NOTIFICATION_ID_RUNNING);
return;
}
int icon = R.drawable.ic_stat_notif;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, null, when);
notification.flags |= Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
// define extended notification area
CharSequence contentTitle = "BatteryFu";
CharSequence contentText = text;
Intent notificationIntent = new Intent(context, ModeSelect.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIFICATION_ID_RUNNING, notification);
}
@Override
public void run() {
if (cancelContext!=null) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) cancelContext.getSystemService(ns);
nMgr.cancel(id);
}
}
protected void raise_notification() {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nm = (NotificationManager) context
.getSystemService(ns);
// nm.cancel( NOTIFICATION_ID ); // tried this, but it just doesn't do
// the trick =(
nm.cancelAll();
String update_file = preferences.getString(UPDATE_FILE, "");
if (update_file.length() > 0) {
setChanged();
notifyObservers(AUTOUPDATE_HAVE_UPDATE);
// raise notification
Notification notification = new Notification(appIcon, appName
+ " update", System.currentTimeMillis());
notification.flags |= NOTIFICATION_FLAGS;
CharSequence contentTitle = appName + " update available";
CharSequence contentText = "Select to install";
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setDataAndType(
Uri.parse("file://"
+ context.getFilesDir().getAbsolutePath() + "/"
+ update_file), ANDROID_PACKAGE);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
nm.notify(NOTIFICATION_ID, notification);
} else {
nm.cancel(NOTIFICATION_ID);
}
}
@SuppressWarnings("NewApi")
private void createNotification(Context context) {
String notificationService = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(notificationService);
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
int port = sharedPreferences.getInt(FTPService.PORT_PREFERENCE_KEY, FTPService.DEFAULT_PORT);
boolean secureConnection = sharedPreferences.getBoolean(FTPService.KEY_PREFERENCE_SECURE, FTPService.DEFAULT_SECURE);
InetAddress address = FTPService.getLocalInetAddress(context);
String iptext = (secureConnection ? FTPService.INITIALS_HOST_SFTP : FTPService.INITIALS_HOST_FTP)
+ address.getHostAddress() + ":"
+ port + "/";
int icon = R.drawable.ic_ftp_light;
CharSequence tickerText = context.getResources().getString(R.string.ftp_notif_starting);
long when = System.currentTimeMillis();
CharSequence contentTitle = context.getResources().getString(R.string.ftp_notif_title);
CharSequence contentText = String.format(context.getResources().getString(R.string.ftp_notif_text), iptext);
Intent notificationIntent = new Intent(context, ExplorerActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
int stopIcon = android.R.drawable.ic_menu_close_clear_cancel;
CharSequence stopText = context.getResources().getString(R.string.ftp_notif_stop_server);
Intent stopIntent = new Intent(FTPService.ACTION_STOP_FTPSERVER);
PendingIntent stopPendingIntent = PendingIntent.getBroadcast(context, 0,
stopIntent, PendingIntent.FLAG_ONE_SHOT);
Notification.Builder notificationBuilder = new Notification.Builder(context)
.setContentTitle(contentTitle)
.setContentText(contentText)
.setContentIntent(contentIntent)
.setSmallIcon(icon)
.setTicker(tickerText)
.setWhen(when)
.setOngoing(true);
Notification notification = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationBuilder.setVisibility(Notification.VISIBILITY_PUBLIC);
notificationBuilder.setCategory(Notification.CATEGORY_SERVICE);
notificationBuilder.setPriority(Notification.PRIORITY_MAX);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notificationBuilder.addAction(stopIcon, stopText, stopPendingIntent);
notificationBuilder.setShowWhen(false);
notification = notificationBuilder.build();
} else {
notification = notificationBuilder.getNotification();
}
// Pass Notification to NotificationManager
notificationManager.notify(NOTIFICATION_ID, notification);
}
private void removeNotification(Context context){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nm = (NotificationManager) context.getSystemService(ns);
nm.cancelAll();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
DbClear dbClear = new DbClear();
int size;
float fontSize;
switch (item.getItemId()) {
case R.id.action_project:
Intent intentProj= new Intent(Intent.ACTION_VIEW, Uri.parse(PROJECT_LINK));
startActivity(intentProj);
break;
case R.id.action_feedsources:
Intent intentfs = new Intent(MainActivity.this, FeedSourcesActivity.class);
intentfs.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intentfs);
break;
case R.id.action_regex:
Intent intentreg = new Intent(MainActivity.this, PrefRegexActivity.class);
intentreg.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intentreg);
break;
case R.id.action_preferences:
Intent intent = new Intent(MainActivity.this, PreferencesActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
break;
case R.id.action_serif:
if (item.isChecked()) {
item.setChecked(false);
mPreferences.edit().putBoolean("serif", false).apply();
} else {
item.setChecked(true);
mPreferences.edit().putBoolean("serif", true).apply();
}
break;
case R.id.action_delNotifies:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) ctx.getSystemService(ns);
nMgr.cancelAll();
break;
case R.id.action_readedFeeds:
dbClear.execute(R.id.action_readedFeeds);
break;
case R.id.action_delFeeds:
dbClear.execute(R.id.action_delFeeds);
break;
case R.id.action_biggerText:
fontSize = mPreferences.getFloat("font_size", AnotherRSS.Config.DEFAULT_FONT_SIZE);
fontSize = fontSize * 1.1f;
mPreferences.edit().putFloat("font_size", fontSize).apply();
break;
case R.id.action_smallerText:
fontSize = mPreferences.getFloat("font_size", AnotherRSS.Config.DEFAULT_FONT_SIZE);
fontSize = fontSize * 0.9f;
if (fontSize < 3.0f) fontSize = 3.0f;
mPreferences.edit().putFloat("font_size", fontSize).apply();
break;
case R.id.action_biggerImageSize:
size = mPreferences.getInt("image_width", AnotherRSS.Config.DEFAULT_MAX_IMG_WIDTH);
size = size + 20;
mPreferences.edit().putInt("image_width", size).apply();
break;
case R.id.action_smallerImageSize:
size = mPreferences.getInt("image_width", AnotherRSS.Config.DEFAULT_MAX_IMG_WIDTH);
size = size - 10;
if (size < 0) size = 0;
mPreferences.edit().putInt("image_width", size).apply();
break;
default:
break;
}
return true;
}
@SuppressLint("StringFormatInvalid")
private void showNotification(final String msg, String tickerText, boolean lowpriority, long when, ConnectionStatus status) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = getIconByConnectionStatus(status);
Notification.Builder nbuilder = new Notification.Builder(this);
if (mProfile != null) nbuilder.setContentTitle(getString(R.string.notifcation_title, mProfile.mName));
else nbuilder.setContentTitle(getString(R.string.notifcation_title_notconnect));
nbuilder.setContentText(msg);
nbuilder.setOnlyAlertOnce(true);
nbuilder.setOngoing(true);
nbuilder.setContentIntent(getLogPendingIntent());
nbuilder.setSmallIcon(icon);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
nbuilder.setColor(Color.GREEN);
}
if (when != 0) nbuilder.setWhen(when);
// Try to set the priority available since API 16 (Jellybean)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
jbNotificationExtras(lowpriority, nbuilder);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
lpNotificationExtras(nbuilder);
}
if (tickerText != null && !tickerText.equals("")){
nbuilder.setTicker(tickerText);
}
@SuppressWarnings("deprecation") Notification notification = nbuilder.getNotification();
mNotificationManager.notify(OPENVPN_STATUS, notification);
startForeground(OPENVPN_STATUS, notification);
// Check if running on a TV
if (runningOnAndroidTV() && !lowpriority) guiHandler.post(new Runnable() {
@Override
public void run() {
if (mlastToast != null) mlastToast.cancel();
String toastText = String.format(Locale.getDefault(), "%s - %s", mProfile.mName, msg);
mlastToast = Toast.makeText(getBaseContext(), toastText, Toast.LENGTH_SHORT);
mlastToast.show();
}
});
}
private void hideNotification() {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(ns);
nMgr.cancelAll();
}
private void showNotification(final String msg, String tickerText, boolean lowpriority, long when, ConnectionStatus status) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
//int icon = getIconByConnectionStatus(status);
int icon = R.drawable.ic_app_notif;
android.app.Notification.Builder nbuilder = new Notification.Builder(this);
if (mProfile != null)
nbuilder.setContentTitle(getString(R.string.notification_title, mProfile.mName));
else
nbuilder.setContentTitle(getString(R.string.notifcation_title_notconnect));
nbuilder.setContentText(msg);
nbuilder.setOnlyAlertOnce(true);
nbuilder.setOngoing(true);
nbuilder.setContentIntent(getLogPendingIntent());
nbuilder.setSmallIcon(icon);
if (when != 0)
nbuilder.setWhen(when);
// Try to set the priority available since API 16 (Jellybean)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
jbNotificationExtras(lowpriority, nbuilder);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
lpNotificationExtras(nbuilder);
if (tickerText != null && !tickerText.equals(""))
nbuilder.setTicker(tickerText);
@SuppressWarnings("deprecation")
Notification notification = nbuilder.getNotification();
mNotificationManager.notify(OPENVPN_STATUS, notification);
startForeground(OPENVPN_STATUS, notification);
// Check if running on a TV
if (runningOnAndroidTV() && !lowpriority)
guiHandler.post(new Runnable() {
@Override
public void run() {
if (mlastToast != null)
mlastToast.cancel();
String toastText = String.format(Locale.getDefault(), "%s - %s", mProfile.mName, msg);
mlastToast = Toast.makeText(getBaseContext(), toastText, Toast.LENGTH_SHORT);
mlastToast.show();
}
});
}
private void setNotification(boolean visible) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
if (visible && mNotificationReceiver == null) {
createNotificationChannel();
int icon = R.drawable.icon;
CharSequence text = "Keyboard notification enabled.";
long when = System.currentTimeMillis();
// TODO: clean this up?
mNotificationReceiver = new NotificationReceiver(this);
final IntentFilter pFilter = new IntentFilter(NotificationReceiver.ACTION_SHOW);
pFilter.addAction(NotificationReceiver.ACTION_SETTINGS);
registerReceiver(mNotificationReceiver, pFilter);
Intent notificationIntent = new Intent(NotificationReceiver.ACTION_SHOW);
PendingIntent contentIntent = PendingIntent.getBroadcast(getApplicationContext(), 1, notificationIntent, 0);
//PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Intent configIntent = new Intent(NotificationReceiver.ACTION_SETTINGS);
PendingIntent configPendingIntent =
PendingIntent.getBroadcast(getApplicationContext(), 2, configIntent, 0);
String title = "Show Hacker's Keyboard";
String body = "Select this to open the keyboard. Disable in settings.";
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.icon_hk_notification)
.setColor(0xff220044)
.setAutoCancel(false) //Make this notification automatically dismissed when the user touches it -> false.
.setTicker(text)
.setContentTitle(title)
.setContentText(body)
.setContentIntent(contentIntent)
.setOngoing(true)
.addAction(R.drawable.icon_hk_notification, getString(R.string.notification_action_settings),
configPendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
/*
Notification notification = new Notification.Builder(getApplicationContext())
.setAutoCancel(false) //Make this notification automatically dismissed when the user touches it -> false.
.setTicker(text)
.setContentTitle(title)
.setContentText(body)
.setWhen(when)
.setSmallIcon(icon)
.setContentIntent(contentIntent)
.getNotification();
notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
mNotificationManager.notify(ID, notification);
*/
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(NOTIFICATION_ONGOING_ID, mBuilder.build());
} else if (mNotificationReceiver != null) {
mNotificationManager.cancel(NOTIFICATION_ONGOING_ID);
unregisterReceiver(mNotificationReceiver);
mNotificationReceiver = null;
}
}
@Override
public void onCreate() {
Log.d(TAG, "onCreate");
String mNotification = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager) getSystemService(mNotification);
mContext = getApplicationContext();
mTorchTask = new TimerTask() {
public void run() {
mPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);
Boolean mPrefScreen = mPreferences.getBoolean(SettingsActivity.KEY_SCREEN, false);
if (mPrefScreen) {
FlashDevice.getInstance(mContext).setFlashMode(FlashDevice.OFF);
} else {
FlashDevice.getInstance(mContext).setFlashMode(FlashDevice.ON);
}
}
};
mTorchTimer = new Timer();
mStrobeRunnable = new Runnable() {
private int mCounter = 4;
public void run() {
int mFlashMode = FlashDevice.ON;
if (FlashDevice.getInstance(mContext).getFlashMode() == FlashDevice.STROBE) {
if (mCounter-- < 1) {
FlashDevice.getInstance(mContext).setFlashMode(mFlashMode);
}
} else {
FlashDevice.getInstance(mContext).setFlashMode(FlashDevice.STROBE);
mCounter = 4;
}
}
};
mStrobeTask = new WrapperTask(mStrobeRunnable);
mStrobeTimer = new Timer();
mSosOnRunnable = new Runnable() {
public void run() {
FlashDevice.getInstance(mContext).setFlashMode(FlashDevice.ON);
mSosTask = new WrapperTask(mSosOffRunnable);
int mSchedTime = 0;
switch (mSosCount) {
case 0:
case 1:
case 2:
case 6:
case 7:
case 8:
mSchedTime = 200;
break;
case 3:
case 4:
case 5:
mSchedTime = 600;
break;
default:
return;
}
if (mSosTimer != null) {
mSosTimer.schedule(mSosTask, mSchedTime);
}
}
};
mSosOffRunnable = new Runnable() {
public void run() {
FlashDevice.getInstance(mContext).setFlashMode(FlashDevice.OFF);
mSosTask = new WrapperTask(mSosOnRunnable);
mSosCount++;
if (mSosCount == 9) {
mSosCount = 0;
}
if (mSosTimer != null) {
mSosTimer.schedule(mSosTask, mSosCount == 0 ? 2000 : 200);
}
}
};
mSosTask = new WrapperTask(mSosOnRunnable);
mSosTimer = new Timer();
}
public static void cancelNotification(Context context) {
// cancel any notifications
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
mNotificationManager.cancel(NOTIFICATION_ID_RUNNING);
}
private void cancelNotification(int notifyId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getSystemService(ns);
nMgr.cancel(notifyId);
}
private void cancelNotification(int notifyId) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getSystemService(ns);
nMgr.cancel(notifyId);
}
@OnClick(R.id.button2)
public void cancelNotification() {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getApplicationContext().getSystemService(ns);
nMgr.cancel(1);
}
private void showNotification(String msg, String tickerText, boolean lowpriority, long when, ConnectionStatus status) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = getIconByConnectionStatus(status);
Notification.Builder nbuilder = new Notification.Builder(this);
if (mProfile != null)
nbuilder.setContentTitle(getString(R.string.notifcation_title, mProfile.mName));
else
nbuilder.setContentTitle(getString(R.string.notifcation_title_notconnect));
nbuilder.setContentText(msg);
nbuilder.setOnlyAlertOnce(true);
nbuilder.setOngoing(true);
nbuilder.setContentIntent(getLogPendingIntent());
nbuilder.setSmallIcon(icon);
if (when != 0)
nbuilder.setWhen(when);
// Try to set the priority available since API 16 (Jellybean)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
jbNotificationExtras(lowpriority, nbuilder);
if (tickerText != null && !tickerText.equals(""))
nbuilder.setTicker(tickerText);
@SuppressWarnings("deprecation")
Notification notification = nbuilder.getNotification();
mNotificationManager.notify(OPENVPN_STATUS, notification);
startForeground(OPENVPN_STATUS, notification);
}