Android:开机自启动并接收推送消息
接收推送消息部分我们通过ZeroMQ实现,可以参考http://www.cnblogs.com/ilovewindy/p/3984283.html。
首先是开机自启动的功能实现,代码如下:
1. AndroidManifest.xml中添加如下代码:
1 <!-- 抓取系统启动事件 --> 2 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 3 4 <application> 5 ….. 6 <service 7 android:name="com.demo.notification.NotificationService" 8 android:icon="@drawable/icon" 9 android:label="@string/app_name" > 10 </service> 11 12 <receiver android:name="com.demo.notification.MyScheduleReceiver" > 13 <intent-filter> 14 <action android:name="android.intent.action.BOOT_COMPLETED" /> 15 </intent-filter> 16 </receiver> 17 </application>
2. 接着是实现MyScheduleReceiver代码,这是当Android启动后会自动启动的程序。
1 public class MyScheduleReceiver extends BroadcastReceiver { 2 3 @Override 4 public void onReceive(Context context, Intent intent) { 5 6 Intent service = new Intent(context, NotificationService.class); 7 context.startService(service); 8 } 9 }
3. 实现NotificationService代码,用来接收推送消息
1 public class NotificationService extends Service { 2 private final IBinder mBinder = new MyBinder(); 3 4 @Override 5 public int onStartCommand(Intent intent, int flags, int startId) { 6 // 将接收推送消息任务放入后台执行 7 new ZeroMQMessageTask().execute(); 8 9 return Service.START_STICKY; 10 } 11 12 @Override 13 public IBinder onBind(Intent arg0) { 14 return mBinder; 15 } 16 17 public class MyBinder extends Binder { 18 public NotificationService getService() { 19 return NotificationService.this; 20 } 21 } 22 23 private class ZeroMQMessageTask extends AsyncTask<String, Void, String> { 24 25 public ZeroMQMessageTask() { 26 } 27 28 @Override 29 protected String doInBackground(String... params) { 30 31 ZMQ.Context context = ZMQ.context(1); 32 ZMQ.Socket subscriber = context.socket(ZMQ.SUB); 33 subscriber.subscribe(ZMQ.SUBSCRIPTION_ALL); 34 subscriber.connect("tcp://x.x.x.x:xxxx"); 35 while (true) { // 通过不终止的循环来保证接收消息 36 message = subscriber.recvStr(); 37 if (!message.equals("0")) { // 0是由我自己定义的空消息标识,可以替换成自定义的其它标识 38 39 // 显示推送消息 40 String ns = Context.NOTIFICATION_SERVICE; 41 NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 42 43 int icon = R.drawable.icon; 44 CharSequence tickerText = "Demo - " + message; 45 long when = System.currentTimeMillis(); 46 47 Notification notification = new Notification(icon, 48 tickerText, when); 49 notification.flags |= Notification.FLAG_AUTO_CANCEL; 50 Context uiContext = getApplicationContext(); 51 CharSequence contentTitle = "Demo"; 52 CharSequence contentText = message; 53 Intent notificationIntent = new Intent(uiContext, 54 NotificationService.class); 55 PendingIntent contentIntent = PendingIntent 56 .getActivity(uiContext, 0, notificationIntent, 0); 57 58 notification.setLatestEventInfo(uiContext, contentTitle, 59 contentText, contentIntent); 60 61 mNotificationManager.notify(1, notification); 62 } 63 } 64 } 65 66 @Override 67 protected void onPostExecute(String result) { 68 } 69 } 70 }
好啦,重新启动手机尝试发条消息吧! :D
服务器端的代码可以参考此文:http://www.cnblogs.com/ilovewindy/p/3998862.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构