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

概述:

首先简单阐述一下我对于消息推送的理解,这里拿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 messagePendingIn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
极光是一款专业的服务平台,可为 Android 等多个平台的应用提供服务。以下是在 Android 平台上使用极光的步骤: 1. 注册极光账号,获取 AppKey 和 Master Secret。 2. 在 Android 项目中添加极光 SDK,可以通过 Maven 或 Gradle 集成。 3. 在 AndroidManifest.xml 文件中添加权限: ``` <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/> <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/> <permission android:name="${applicationId}.permission.JPUSH_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="${applicationId}.permission.JPUSH_MESSAGE" /> ``` 4. 在 AndroidManifest.xml 文件中添加以下代码,注册 JPushReceiver 和 JPushService: ``` <receiver android:name="cn.jpush.android.service.PushReceiver" android:enabled="true" android:exported="false"> <intent-filter> <action android:name="cn.jpush.android.intent.RECEIVE_REGISTRATION_ID" /> <action android:name="cn.jpush.android.intent.MESSAGE_RECEIVED" /> <action android:name="cn.jpush.android.intent.NOTIFICATION_RECEIVED" /> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED" /> <action android:name="cn.jpush.android.intent.ACTION_RICHPUSH_CALLBACK" /> <action android:name="cn.jpush.android.intent.CONNECTION" /> <category android:name="${applicationId}" /> </intent-filter> </receiver> <receiver android:name=".receiver.JPushNotificationClickReceiver"> <intent-filter> <action android:name="cn.jpush.android.intent.NOTIFICATION_OPENED_PROXY" /> <category android:name="${applicationId}" /> </intent-filter> </receiver> <service android:name="cn.jpush.android.service.PushService" android:enabled="true" android:exported="false" android:process=":pushcore"> <intent-filter> <action android:name="cn.jpush.android.intent.REGISTER" /> <action android:name="cn.jpush.android.intent.REPORT" /> <action android:name="cn.jpush.android.intent.PushService" /> <action android:name="cn.jpush.android.intent.PUSH_TIME" /> </intent-filter> </service> ``` 5. 在 Application 类中初始化 JPush SDK: ``` JPushInterface.setDebugMode(true); // 调试模式 JPushInterface.init(this); ``` 6. 在后台中创建任务,设置目标、内容等参数。 7. 在应用中接收消息,可以通过 JPushReceiver 中的回调方法获取消息,然后进行处理。 以上就是在 Android 平台上使用极光的步骤。需要注意的是,在使用极光时,需要保证网络连接正常,并且在应用退出时需要调用 JPushInterface.stopPush() 方法停止服务。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值