重启后android通知 [英] android notification after reboot
问题描述
我想在通知栏中放置一个通知,按下时将启动我的应用程序.虽然我这样做没有问题,但我的用户也希望在重新启动后出现通知.他们有来自其他供应商的应用程序可以执行此操作.
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通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!