需要的知识点:Notification、Service

第三方开源框架 : android-async-http-master

推送的来源:android项目中,有时会有这样一种需求:客户每隔一段时间,就像服务器发送一个请求,以获取某些重要的、实时更新的消息。比如版本更新,请求是否重新下载等。

关键点在于: 如何让应用实现在后台一直处于运行状态,并且每个一段时间就向服务器发一个请求?

一、在Activity中,通过startService()来启动服务

     public void open(View view) {

        Intent intent = new Intent(this, PushSmsService.class); //跳转页面 
        startService(intent);  // 启动服务  
    }
 
二、自定义一个服务类,去继承android的Service类
    /*******************************************************/
    //短信推送服务类,在后台长期运行,每个一段时间就向服务器发送一次请求 
    /*******************************************************/
   public class PushSmsService extends Service {

    private MyThread myThread; //线程  
    private NotificationManager manager;  //notificationManager  
    private Notification notification;  //notification
    private PendingIntent pi;  //intent
    private AsyncHttpClient client;  //异步http客户端
    private boolean flag = true;  //标志位为true
  
    @Override  
    public IBinder onBind(Intent intent) {  
        return null;  
    }  
 
/****************************************< 加载页面 >******************************************/
   @Override

    public void onCreate() {  
        System.out.println("oncreate()");  
        this.client = new AsyncHttpClient();  
        this.myThread = new MyThread();  
        this.myThread.start();  //启动线程
        super.onCreate();  
    }  
 
    //销毁页面
    public void onDestroy() {

        this.flag = false; //标志位为:false  
        super.onDestroy();  
    }  
 
 
/****************************************< notification >******************************************/
    private void notification(String content, String number, String date) {

        manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  // 获取系统的通知管理器  
        notification = new Notification(R.drawable.ic_menu_compose, content,   System.currentTimeMillis());  //获取图标、内容、时间等等
        notification.defaults = Notification.DEFAULT_ALL; // 使用默认设置(比如铃声、震动、闪灯)  
        notification.flags = Notification.FLAG_AUTO_CANCEL; // 但用户点击消息后,消息自动在通知栏自动消失  
        notification.flags |= Notification.FLAG_NO_CLEAR;// 点击通知栏的删除,消息不会依然不会被删除  
  
        Intent intent = new Intent(getApplicationContext(),   ContentActivity.class);  //跳转页面
        intent.putExtra("content", content);  //封装内容
        intent.putExtra("number", number);  //封装数字
        intent.putExtra("date", date);  //封装时间
  
        pi = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);           
        notification.setLatestEventInfo(getApplicationContext(), number  + "发来短信", content, pi);  
        manager.notify(0, notification);  // 将消息推送到状态栏  
       
/****************************************< 线程 >******************************************/
        private class MyThread extends Thread {

        public void run() {  
                 String url = "http://110.65.99.66:8080/jerry/PushSmsServlet";  //定位服务器
            while (flag) {//检查并判断标志位  
                System.out.println("发送请求");  
                try {                      
                    Thread.sleep(10000); // 每个10秒向服务器发送一次请求   
                } catch (InterruptedException e)
               {  
                    e.printStackTrace();  
                }  
  
                // 采用get方式向服务器发送请求  
                client.get(url, new AsyncHttpResponseHandler() {   
                    public void onSuccess(int statusCode, Header[] headers,  byte[] responseBody) {  
                        try {  
                            JSONObject result = new JSONObject(new String(  responseBody, "utf-8"));  
                            int state = result.getInt("state");  
                            // 假设偶数为未读消息  
                            if (state % 2 == 0) {  
                                String content = result.getString("content");  
                                String date = result.getString("date");  
                                String number = result.getString("number");  
                                notification(content, number, date);  
                            }  
                        } catch (Exception e) {  
                            e.printStackTrace();  
                        }   
                    }  
 
               public void onFailure(int statusCode, Header[] headers,   byte[] responseBody, Throwable error) {

                        Toast.makeText(getApplicationContext(), "数据请求失败", 0)  .show();  
                    }  
        });  
    }  
  
三、清单文件中注册
      3.1 service注册:
            <service android:name=".PushSmsService"></service>
四、权限配置问题
       4.1 由于通知信息使用到了手机的震动功能和网络访问,所以需要配置权限。
             <uses-permission android:name="android.permission.VIBRATE"/>  <!-震动权限->

             <uses-permission android:name="android.permission.INTERNET"/>  <!-网络权限->
五、再定义Activity
     (用于用户点击下拉通知栏里的某条消息后,跳转到详细的消息界面)
      public class ContentActivity extends Activity {

          protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_content);  
            Intent intent = getIntent();  
            TextView tv_content = (TextView) this.findViewById(R.id.tv_content);  
            TextView tv_number = (TextView) this.findViewById(R.id.tv_number);  
            TextView tv_date = (TextView) this.findViewById(R.id.tv_date);  
        if (intent != null) {  
            String content = intent.getStringExtra("content");  
            String number = intent.getStringExtra("number");  
            String date = intent.getStringExtra("date");  
            tv_content.setText("内容:" + content);  
            tv_number.setText("号码:" + number);  
            tv_date.setText("日期:" + date);  
        }   
    }  
  
    5.1 在清单文件中注册新的Activity
          <activity android:name=".ContentActivity"></activity> 
    5.2 注意到上边的代码是需要服务器提供支持的,我们去新建一个简单的服务器,里边添加一个Servlet和一个实体类就可以了
         public class PushSmsServlet extends HttpServlet {

         private static int index = 1;  
        public void doGet(HttpServletRequest request, HttpServletResponse response)  
              throws ServletException, IOException {  
              System.out.println(index);  
              // List<Sms> smsList = new ArrayList<Sms>();  
              Sms sms = new Sms(index,  "傻逼" + index, "2016-08-08",  "15208432222");  
              index++;  
             // smsList.add(sms);  
             // sms = new Sms(0, "傻逼", "2016-08-08", "15208432222");  
             // smsList.add(sms);  
             // sms = new Sms(1, "傻逼", "2016-08-08", "13522224444");  
             // smsList.add(sms);  
  
        response.setContentType("text/html");  
        request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("utf-8");  
        PrintWriter out = response.getWriter();  
        Gson gson = new Gson();  
        // 将Sms类的数据转换为json数据格式  
        String json = gson.toJson(sms);  
        out.write(json);  
        out.flush();  
        out.close();  
    }  
 
       //doPost
        public void doPost(HttpServletRequest request, HttpServletResponse response)   throws ServletException, IOException {

           this.doGet(request, response);  
       }
     
/****************************************< 定义封装用于存放消息的类 >******************************************/    

public class Sms {

    private int state;  
    private String content;  
    private String date;  
    private String number;  
  
    public Sms(int state, String content, String date, String number) {  
        super();  
        this.state = state;  
        this.content = content;  
        this.date = date;  
        this.number = number;  
    }    
 
这部只是分核心代码,其余的就请自行扩充!

Android开发--推送的更多相关文章

  1. Android消息推送怎么实现?

    在开发Android和iPhone应用程序时,我们往往需要从服务器不定的向手机客户端即时推送各种通知消息,iPhone上已经有了比较简单的和完美的推送通知解决方案,可是Android平台上实现起来却相 ...

  2. Android实现推送方式解决方案(转)

    本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅读最新的新闻信息. ...

  3. Android消息推送完美方案[转]

    转自 Android消息推送完美方案 推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原因,Android消息推送我们不得不大费周折.本文就是用来 ...

  4. Android消息推送完美方案

    转自:http://bbs.hiapk.com/thread-4652657-1-1.html 推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原 ...

  5. Android消息推送完美解决方案全析

    推送功能在手机应用开发中越来越重要,已经成为手机开发的必须.在Android应用开发中,由于众所周知的原因,Android消息推送我们不得不大费周折.本文就是用来和大家共同探讨一种Android消息推 ...

  6. Android消息推送(二)--基于MQTT协议实现的推送功能

    国内的Android设备,不能稳定的使用Google GCM(Google Cloud Messageing)消息推送服务. 1. 国内的Android设备,基本上从操作系统底层开始就去掉了Googl ...

  7. 【转】Android实现推送方式解决方案

    本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅读最新的新闻信息. ...

  8. Android实现推送方式解决方案 - 长连接+心跳机制(MQTT协议)

    本文介绍在Android中实现推送方式的基础知识及相关解决方案.推送功能在手机开发中应用的场景是越来起来了,不说别的,就我们手机上的新闻客户端就时不j时的推送过来新的消息,很方便的阅读最新的新闻信息. ...

  9. android 消息推送

    android 消息推送 极光推送百度云推送(语音)友盟消息推送

随机推荐

  1. insert时出现主键冲突的处理方法【转载】

    原文出处:http://hi.baidu.com/ytjwt/blog/item/1ccc2c26022b0608908f9d8c.html 使用"insert into"语句进行 ...

  2. leetcode面试准备: Jump Game II

    1 题目 Given an array of non-negative integers, you are initially positioned at the first index of the ...

  3. Android开发详解之onTouch和onClick详解

    android组件中的onTouch,onClick,onLongClick事件发生先后顺序和关联: 一,onTouch返回false 首先是onTouch事件的down事件发生,此时,如果长按,触发 ...

  4. 【转】在ubuntu环境下搭建svn server遇到的一些问题

    原文网址:http://www.cnblogs.com/pcchinadreamfly/archive/2012/11/24/2786046.html 前段时间在ubuntu 12.04lts上倒腾了 ...

  5. HDU-2188 悼念512汶川大地震遇难同胞——选拔志愿者

    http://acm.hdu.edu.cn/showproblem.php?pid=2188 巴什博奕(Bash Game)的转换:换一种说法而已 悼念512汶川大地震遇难同胞——选拔志愿者 Time ...

  6. Hibernate的Session会话中get()和load()方法的区别

    1.get和load都可以从数据库中获取数据 .get拿到的是真的对象,load拿到的是代理对象 2.get和load从数据库中获取数据,如果获取不到,get返回null,load会出现ObjectN ...

  7. 高性能I/O设计模式Reactor和Proactor

    系统I/O 可分为阻塞型, 非阻塞同步型,非阻塞异步型. (Linux对aio支持的不完整,所以linux上用Reactor比较多:Proactor需要系统API支持真正的“异步”) 阻塞型I/O意味 ...

  8. 《A First Course in Probability》-chape4-离散型随机变量-方差

    为了描述一组数据的均值,我们引入了数学期望的概念,为了描述一组数据相对均值的波动情况,我们引入了方差. 能够看到,方差的本质也是一个期望,那么我们就能够利用期望的定义将其继续展开. 方差的一条重要性质 ...

  9. hdoj 1285 确定比赛名次【拓扑排序】

    确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  10. 385. Mini Parser

    括号题一般都是stack.. 一开始想的是存入STACK的是SRING,然后POP出括号在构建新的NestedInteger放到另一个里面,但是操作起来费时费力. 后来猛然发现其实可以直接吧Neste ...