Android 通過Service單獨進程模仿離線推送 Server Push

來源:互聯網
上載者:User

概述:

         首先簡單闡述一下我對於訊息推送的理解,這裡拿QQ來舉例吧,當我們手機端的QQ離線了,並且退出了QQ應用,但是這時候如果別人給我們發了資訊,我們沒有上線。伺服器會將寄件者發送的資訊推送過來然後我們發布通知來顯示通知我們的使用者

 

 

原理簡單闡述:

         通過以上概述,我們基本瞭解我們需要一個獨立進程的後台服務,在AndroidManifest

.xml中註冊Service時,有一個android:process屬性這個屬性有2種情況,即為.和:兩種,其中.代表為此服務開啟一個全域的獨立進程,如果以:開頭則為此服務開啟一個為此應用私人的獨立進程

 

 

編碼實現:


ServerPushService檔案:

[java]
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
 
public class ServerPushService extends Service{ 
    //擷取訊息線程  
    private MessageThread messageThread = null; 
    //點擊查看  
    private Intent messageIntent = null; 
    private PendingIntent messagePendingIntent = null; 
    //通知欄訊息  
    private int messageNotificationID = 1000; 
    private Notification messageNotification = null; 
    private NotificationManager messageNotificationManager = null; 
    @Override 
    public IBinder onBind(Intent intent) { 
        return null; 
    } 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
        //初始化  
        messageNotification = new Notification(); 
        messageNotification.icon = R.drawable.ic_launcher;  //通知圖片  
        messageNotification.tickerText = "新訊息";         //通知標題  
        messageNotification.defaults = Notification.DEFAULT_SOUND; 
        messageNotificationManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE); 
        //點擊查看  
        messageIntent = new Intent(this,MessageActivity.class); 
        messagePendingIntent = PendingIntent.getActivity(this, 0, messageIntent, 0); 
        //開啟線程  
        MessageThread thread = new MessageThread(); 
        thread.isRunning = true; 
        thread.start(); 
        return super.onStartCommand(intent, flags, startId); 
    } 
 
    /***
     * 從服務端擷取訊息
     * @author zhanglei
     *
     */ 
    class MessageThread extends Thread{ 
        //運行狀態  
        public boolean isRunning = true; 
        @Override 
        public void run() { 
            while(isRunning){ 
                try { 
                    //休息10秒  
                    Thread.sleep(10000); 
                    if(getServerMessage().equals("yes")){ 
                        //設定訊息內容和標題  
                        messageNotification.setLatestEventInfo(ServerPushService.this, "您有新訊息!", "這是一條新的測試訊息", messagePendingIntent); 
                        //發布訊息  
                        messageNotificationManager.notify(messageNotificationID, messageNotification); 
                        //避免覆蓋訊息,採取ID自增  
                        messageNotificationID++; 
                    } 
                } catch (Exception e) { 
                    e.printStackTrace(); 
                } 
            }    
        } 
    } 
    /***
     * 類比了服務端的訊息。實際應用中應該去伺服器拿到message
     * @return
     */ 
    public String getServerMessage(){ 
        return "yes"; 
    } 

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

public class ServerPushService extends Service{
 //擷取訊息線程
 private MessageThread messageThread = null;
 //點擊查看
 private Intent messageIntent = null;
 private PendingIntent messagePendingIntent = null;
 //通知欄訊息
 private int messageNotificationID = 1000;
 private Notification messageNotification = null;
 private NotificationManager messageNotificationManager = null;
 @Override
 public IBinder onBind(Intent intent) {
  return null;
 }
 @Override
 public int onStartCommand(Intent intent, int flags, int startId) {
  //初始化
  messageNotification = new Notification();
  messageNotification.icon = R.drawable.ic_launcher; //通知圖片
  messageNotification.tickerText = "新訊息";   //通知標題
  messageNotification.defaults = Notification.DEFAULT_SOUND;
  messageNotificationManager = (NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
  //點擊查看
  messageIntent = new Intent(this,MessageActivity.class);
  messagePendingIntent = PendingIntent.getActivity(this, 0, messageIntent, 0);
  //開啟線程
  MessageThread thread = new MessageThread();
  thread.isRunning = true;
  thread.start();
  return super.onStartCommand(intent, flags, startId);
 }

 /***
  * 從服務端擷取訊息
  * @author zhanglei
  *
  */
 class MessageThread extends Thread{
  //運行狀態
  public boolean isRunning = true;
  @Override
  public void run() {
   while(isRunning){
    try {
     //休息10秒
     Thread.sleep(10000);
     if(getServerMessage().equals("yes")){
      //設定訊息內容和標題
      messageNotification.setLatestEventInfo(ServerPushService.this, "您有新訊息!", "這是一條新的測試訊息", messagePendingIntent);
      //發布訊息
      messageNotificationManager.notify(messageNotificationID, messageNotification);
      //避免覆蓋訊息,採取ID自增
      messageNotificationID++;
     }
    } catch (Exception e) {
     e.printStackTrace();
    }
   } 
  }
 }
 /***
  * 類比了服務端的訊息。實際應用中應該去伺服器拿到message
  * @return
  */
 public String getServerMessage(){
  return "yes";
 }
}


註冊該service在一個單獨的進程中

[html]
<!-- 為此應用私人的獨立進程 --> 
        <service  
                android:name="com.jay.serverpush.ServerPushService" 
                android:process=":message" 
            > 
</service> 

<!-- 為此應用私人的獨立進程 -->
        <service
             android:name="com.jay.serverpush.ServerPushService"
             android:process=":message"
            >
</service>


說明:該檔案編寫了一個service用於後台運行,在manifest裡面將該service聲明成progress為:開頭的,這樣在一個單獨的進程裡面運行,以實現在程式關閉之後達到進程不關閉的目的以此來實現離線推送的目的,編碼中的注釋很明確,掃描伺服器、判斷邏輯發布通知等,注釋很明確在此不在闡述

 


開啟服務的方式:

[java]
Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        this.startService(new Intent(this,ServerPushService.class)); 
    } 
 
 
this.startService(new Intent(this,ServerPushService.class)); 

@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  this.startService(new Intent(this,ServerPushService.class));
 }


this.startService(new Intent(this,ServerPushService.class));


通過這句話在第一次進入oncreate方法就開啟了單獨進程的服務

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.