TesterHome android app 编写历程(五)

来源:互联网 发布:程序员刚入职很痛苦 编辑:程序博客网 时间:2024/04/28 21:03

前言


这次主要还是会继续上一次关于TesterHome的登录授权,这次的话会涉及到一些具体的实现。另外还会简单的说一下EventBus的用法。

正文


上一章节我们已经说过,想要过去到Authorization Code的话就要通过https://testerhome.com/oauth/authorize?response_type=code&client_id=402e7adf&redirect_uri=urn:ietf:wg:oauth:2.0:oob 进行访问,可是问题来了,Android的webview设置了loadurl以后,仍然会去调用系统的浏览器去访问我们的网页,所以这里就需要实现一个WebViewClient的匿名内部类重写里面的shouldOverrideUrlLoading方法,具体代码如下

webView.loadUrl(authUrl);webView.setWebViewClient(new WebViewClient() {    @Override    public boolean shouldOverrideUrlLoading(WebView view, String url) {        Log.d(TAG, url);        if (url.equals("http://testerhome.com/")) {            url = authUrl;        }        if (url.contains("http://testerhome.com/oauth/authorize/")) {            String code[] = url.split("http://testerhome.com/oauth/authorize/");            Log.d(TAG, code[1]);            TesterHomeOuthApi.getInstance().getTopicsService().getAccessToken(code[1], Config.AUTHORRIZE_GRANT_TYPE, Config.AUTHORRIZE_CLIENTID,                    Config.AUTHORRIZE_CLIENT_SECRET, Config.AUTHORRIZE_REDIRECT_URL, new Callback<Outh>() {                        @Override                        public void success(Outh outh, Response response) {                            Log.d(TAG, outh.getAccess_token());                            PreferenceUtils.setPrefString(getActivity(), "access_token", outh.getAccess_token());                            PreferenceUtils.setPrefString(getActivity(),"refresh_token",outh.getRefresh_token());                            getUserInfo(outh.getAccess_token());                        }                        @Override                        public void failure(RetrofitError error) {                        }                    });            view.setVisibility(View.GONE);        }        view.loadUrl(url); // 在当前的webview中跳转到新的url        return true;    }    @Override    public void onPageFinished(WebView view, String url) {    }});

以上出现那么几个判断主要是因为获取授权码首先需要进行登录,但是登录以后默认会跳转到首页去,这个时候我就需要引导它到我需要的页面去,并且获取到授权码后页面又显示出了授权码的页面,这个也并不是我想要显示给用户看的,我只是需要获取到授权码再进行获取access_token就可以了,所以当它进行访问授权码页面时,我们重新又对访问的链接进行修改,让呈现给用户的是一个友好的界面。这里就基本介绍完了登录授权的过程。

EventBus的使用


EventBus的好处就是在于组件与组件间的解耦。这里用到EventBus的地方是在与应用软件运行时,会通过application类运行一个service服务,service服务每隔2个小时会进行通知数据的获取,并将该数据与数据库中的数据进行比较是否有新增数据,如果有的话就会在相应的控件上显示未读通知个数。
下来我们看看发布的内容,这里我们只要去发布一个未读的个数就可以了,所以我们需要新建一个类。

public class NotificationEvent {    private int unreadCount;    public NotificationEvent(int unreadCount){        this.unreadCount = unreadCount;    }    public int getUnreadCount(){        return this.unreadCount;    }}

再来看看我们的内容发布者

TesterHomeApi.getInstance().getTopicsService().getNotifications(access_token, 0, new Callback<NotificationResponse>() {    @Override    public void success(NotificationResponse notificationResponse, Response response) {        for(int i=0;i<notificationResponse.getNotifications().size();i++){            Notification notification = notificationResponse.getNotifications().get(i);            QueryBuilder<NotificationDB> qb = notificationDBDao.queryBuilder();            qb.where(TopicDBDao.Properties.Id.eq(notification.getId()));            if(qb.list().size()==0){                unreadCount+=1;            }        }        EventBus.getDefault().post(new NotificationEvent(unreadCount));        if (unreadCount!=0){            PendingIntent pendingIntent = PendingIntent.getActivity(DataService.this, 0, new Intent(DataService.this, MainActivity.class), 0);            android.app.Notification notification  = new android.app.Notification.Builder(DataService.this)                    .setSmallIcon(R.drawable.ic_launcher)                    .setTicker("有新的通知"+unreadCount+"条,请查看")                    .setContentTitle("Testerhome 通知")                    .setContentText("新通知")                    .setContentIntent(pendingIntent).setNumber(1).build();            notification.flags |= android.app.Notification.FLAG_AUTO_CANCEL;            manager.notify(NOTIFICATION_FLAG, notification);        }    }    @Override    public void failure(RetrofitError error) {    }});

这里主要看一句就可以了EventBus.getDefault().post(new NotificationEvent(unreadCount));
通过post发送实例化的对象。

下来我们看看接受者需要做什么呢,很简单。注册EventBus,并且实现Event实现就可以了
在oncreate中

EventBus.getDefault().register(this);
 public void onEvent(NotificationEvent notificationEvent){        unreadCount = notificationEvent.getUnreadCount();        badgeView.setBadgeCount(unreadCount); }

就这么简单就实现了未读通知了,不过目前实现还是有点简单,根本就是没有去判断通知是否已阅等等。

0 0
原创粉丝点击