一、java代码
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Intent playIntent = new Intent(this, TimeActivity.class); playIntent.putExtra("参数标示位", leftTime + ""); PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, playIntent, PendingIntent.FLAG_UPDATE_CURRENT); builder = new NotificationCompat.Builder(getApplicationContext()); builder.setOngoing(true); builder.setContentTitle("通知名称"); builder.setContentText("请通知简介"); builder.setSmallIcon(R.drawable.icon_onlinestudy); builder.setContentIntent(pendingIntent) builder.setAutoCancel(true); builder.setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE);//统一消除声音和震动 manager.notify(0, builder.build());
说明:
1:Intent playIntent = new Intent(this, TimeActivity.class);是实现点击通知栏的跳转
2:builder.setOngoing(true)是设置点击不可以不消失,一直显示
2:builder.setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE);是设置消除声音
二、结合service的使用
public class MyService extends Service { private boolean running = false; //默认显示的信息 private String info = "正在运行"; public MyService() { } public class Binder extends android.os.Binder { void setInfo(String info) { MyService.this.info = info; } public MyService getService() { return MyService.this; } } @Override public IBinder onBind(Intent intent) { return new Binder(); } @Override public boolean onUnbind(Intent intent) { return super.onUnbind(intent); } @Override public int onStartCommand(Intent intent, int flags, int startId) { // uploadFile(); return super.onStartCommand(intent, flags, startId); } // 图片上传表示位 private int imageFlagNumber = 0; @Override public void onCreate() { //service在第一次创建时调用 super.onCreate(); running = true; imageFlagNumber = 0; handler.postDelayed(runnable, 1000); } private Handler handler = new Handler(); private Runnable runnable = new Runnable() { @Override public void run() { uploadFile(); handler.postDelayed(this, 5000); } }; @Override public void onDestroy() { //关闭服务 super.onDestroy(); running = false; } //设置回调 private CallBack callBack; public void setCallBack(CallBack callBack) { this.callBack = callBack; } public CallBack getCallBack() { return callBack; } public interface CallBack { void updateData(String data); } public Gson mGson = new Gson(); NotificationManager manager; public FinalHttp fh = new FinalHttp(); private List<DoModel> list_result = new ArrayList<DoModel>(); private void uploadFile() { AjaxParams params = new AjaxParams(); params.put("doUserId", MemberUserUtils.getUid(this)); String url = Consts.URL + Consts.APP.MessageAction + "?action_flag=listDoTip"; fh.configTimeout(Consts.TIME_OUT); fh.post(url, params,new AjaxCallBack<String>() { @Override public void onSuccess(String response) { // TODO 网络访问成功 super.onSuccess(response); Log.i("pony_log", "返回的数据信息是:" + response); try { JSONObject jo = new JSONObject(response); ResponseEntry entry = new ResponseEntry(); entry.setRepCode(jo.optString("repCode")); entry.setRepMsg(jo.optString("repMsg")); if (jo.optString("repCode").equals("666")) { String dataInfor = jo.optString("data"); if (null !=dataInfor && !TextUtils.isEmpty(dataInfor)) { list_result = mGson.fromJson(dataInfor, new TypeToken<List<DoModel>>() { }.getType()); manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); for(int i=0;i<list_result.size();i++){ Intent playIntent = new Intent(getApplicationContext(), ReviewShareMessageActivity.class); playIntent.putExtra("msg",list_result.get(i).getDoName()); PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 1, playIntent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext()); builder.setContentTitle("事件提醒") .setContentText(list_result.get(i).getDoName()+"结束时间"+list_result.get(i).getDoDate()) .setSmallIcon(R.drawable.icon_onlinestudy) .setDefaults(Notification.DEFAULT_ALL) .setAutoCancel(true) .setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE);//统一消除声音和震动 // .setContentIntent(pendingIntent) manager.notify((i+1), builder.build()); } } } // updateTip(); } catch (JSONException e) { e.printStackTrace(); } } }); } private void updateTip() { AjaxParams params = new AjaxParams(); params.put("userId", MemberUserUtils.getUid(this)); String url = Consts.URL + Consts.APP.MessageAction + "?action_flag=UpdateTip"; fh.configTimeout(Consts.TIME_OUT); fh.post(url, params,new AjaxCallBack<String>() { @Override public void onSuccess(String response) { // TODO 网络访问成功 super.onSuccess(response); Log.i("pony_log", "返回的数据信息是:" + response); } }); } }
三、启动服务
Intent intent = new Intent(this, MyService.class); startService(intent);
四、AndroidManifest注册
<service android:name="com.pony.service.MyService" android:enabled="true" android:exported="true" />