重启后android通知

编程入门 行业动态 更新时间:2024-10-25 10:34:34
本文介绍了重启后android通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在通知栏中放置一个通知,按下时将启动我的应用程序.虽然我这样做没有问题,但我的用户也希望在重新启动后出现通知.他们有来自其他供应商的应用程序可以执行此操作.

I want to put up a notification in the notification bar that will launch my app when pressed. While I have no problems doing this, my users want the notification to come up after a reboot as well. They have an app from another vendor that does this.

我能找到的一切都表明应用程序必须正在运行才能显示通知.有什么想法吗?

Everything I can find states that the app must be running for the notification to display. Any ideas?

推荐答案

您需要添加一个在重启后启动服务的接收器.

You need to add a receiver that launches a Service after a reboot.

在您的清单中注册 Boot Complete

In your manifest register for Boot Complete

<service android:name="com.meCorp.service.MeCorpServiceClass"/> ... <receiver android:name="com.meCorp.receiver.MyRebootReceiver"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED"/> </intent-filter> </receiver> ... <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

在您的启动接收器中,启动一项服务.

In your boot receiver, launch a service.

public class MyRebootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent serviceIntent = new Intent(context, MeCorpServiceClass.class); serviceIntent.putExtra("caller", "RebootReceiver"); context.startService(serviceIntent); } }

这是一个在后台运行的服务类的示例.

Here is an example for a service class to run in the background.

public class MeCorpServiceClass extends IntentService{ @Override protected void onHandleIntent(Intent intent){ String intentType = intent.getExtras().getString("caller"); if(intentType == null) return; if(intentType.Equals("RebootReceiver")) //Do reboot stuff //handle other types of callers, like a notification. } }

或者只需使用 Urban AirShip 等第三方,它会为您处理所有这些.

OR Just use a third party like Urban AirShip, which handles all that for you.

更多推荐

重启后android通知

本文发布于:2023-11-27 01:49:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636072.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重启   通知   android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!