Android——自定义通知栏使用
2015-11-27 17:46
399 查看
转载请注明转自:[noyet12的博客](http://blog.csdn.net/u012975705) 博客原址:http://blog.csdn.net/u012975705/article/details/50073197
Android中通知栏的使用,还是刚学Android时玩过,后面一直没机会用到,今天做项目的时候用到了,这里mark下。
通知栏写法:
package com.plusub.renthostapp.service; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.IBinder; import android.support.annotation.Nullable; import android.widget.RemoteViews; import com.plusub.lib.service.BaseService; import com.plusub.lib.task.TaskMessage; import com.plusub.renthostapp.R; import com.plusub.renthostapp.activity.MainActivity; /** * package: com.plusub.renthostapp.service * Created by noyet on 2015/11/27. */ public class NotifyService extends BaseService { private static NotificationManager mManager; private Notification mNotification; private RemoteViews mRemoteViews; private final static String RENT_NEWS = "有新的租地订单"; private final static String SHOP_NEWS = "有新的商品订单"; private final static String LOG_NEWS = "有新的日志"; public static void cancelNotify() { mManager.cancelAll(); } public static void startService(Context context) { Intent intent = new Intent(context, NotifyService.class); context.startService(intent); } public static void startService(Context context, Intent intent) { context.startService(intent); } @Override public void onCreate() { super.onCreate(); mManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); mRemoteViews = new RemoteViews(getPackageName(), R.layout.notify_item1); mNotification = new Notification(R.drawable.ic_launcher, "有新消息", System.currentTimeMillis()); mNotification.defaults |= Notification.DEFAULT_SOUND; mNotification.flags |= Notification.FLAG_AUTO_CANCEL; } @Override public int onStartCommand(Intent intent, int flags, int startId) { int logsNum = intent.getIntExtra("logsNum", 0); int orderNum = intent.getIntExtra("orderNum", 0); int rentNum = intent.getIntExtra("rentNum", 0); Intent notifyIntent = new Intent(this, MainActivity.class); if (rentNum > 0) { mRemoteViews.setTextViewText(R.id.notify_item_tv, RENT_NEWS); mNotification.contentView = mRemoteViews; notifyIntent.putExtra("fragment", 0); // notifyIntent.putExtra("notify_text", RENT_NEWS); mNotification.contentIntent = PendingIntent.getActivity(this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT); mManager.notify(0, mNotification); } if (orderNum > 0) { mRemoteViews.setTextViewText(R.id.notify_item_tv, SHOP_NEWS); mNotification.contentView = mRemoteViews; notifyIntent.putExtra("fragment", 1); // notifyIntent.putExtra("notify_text", SHOP_NEWS); mNotification.contentIntent = PendingIntent.getActivity(this, 1, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT); mManager.notify(1, mNotification); } if (logsNum > 0) { mRemoteViews.setTextViewText(R.id.notify_item_tv, LOG_NEWS); mNotification.contentView = mRemoteViews; notifyIntent.putExtra("fragment", 0); // notifyIntent.putExtra("notify_text", LOG_NEWS); mNotification.contentIntent = PendingIntent.getActivity(this, 2, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT); mManager.notify(2, mNotification); } return super.onStartCommand(intent, flags, startId); } @Override public void exitApp() { mManager.cancelAll(); stopSelf(); } @Override public boolean isAutoFinish() { return false; } @Override public void refresh(TaskMessage taskMessage, Object... objects) { } @Nullable @Override public IBinder onBind(Intent intent) { return null; } }
App不可见时启动服务
@Override protected void onPause() { super.onPause(); if (mTimer != null) { mTimer.cancel(); } if (mTask != null) { mTask.cancel(); } if (MainApplication.getInstance().getIsLogin()) { mTask = new TimerTask() { @Override public void run() { getNews(); } }; mTimer = new Timer(); mTimer.schedule(mTask, 3 * 1000, 3 * 60 * 1000); } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories