日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 通过Service单独进程模仿离线推送 Server Push

發布時間:2024/9/20 Android 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 通过Service单独进程模仿离线推送 Server Push 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

概述:

???????? 首先簡單闡述一下我對于消息推送的理解,這里拿QQ來舉例吧,當我們手機端的QQ離線了,并且退出了QQ應用,但是這時候如果別人給我們發了信息,我們沒有上線。服務器會將發送者發送的信息推送過來然后我們發布通知來顯示通知我們的用戶

?

?

原理簡單闡述:

???????? 通過以上概述,我們基本了解我們需要一個獨立進程的后臺服務,在AndroidManifest

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

?

?

編碼實現:

ServerPushService文件:

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在一個單獨的進程中

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



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

@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方法就開啟了單獨進程的服務

轉載于:https://my.oschina.net/u/1262457/blog/179425

總結

以上是生活随笔為你收集整理的Android 通过Service单独进程模仿离线推送 Server Push的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。