Android开发之NotificationManager的使用

一、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" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android毕业设计源码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值