在 Android 中实现 startForeground 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3687200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implement startForeground method in Android
提问by Jovan
I want to implement the startForeground()
method in the Service
class for prevent service self kill.
我想startForeground()
在Service
类中实现防止服务自杀的方法。
Can anybody sent me code for implementing this method?
任何人都可以向我发送实现此方法的代码吗?
回答by Someone Somewhere
Jovan, here is a method for making compatible code for 2.0+ and 1.6- (the code shows you how to detect which one is compatible) http://android-developers.blogspot.com/2010/02/service-api-changes-starting-with.html
Jovan,这里有一种为 2.0+ 和 1.6- 制作兼容代码的方法(代码告诉你如何检测哪个兼容) http://android-developers.blogspot.com/2010/02/service-api-changes -starting-with.html
For 2.0+, I put together some example code (using startForeground). Observe that some code is now deprecated, however, Notification.Builder uses API level 11 (3.x) which means I won't use that until most phones use a compatible Android version. Since the vast majority of phones is now running some version of 2.x, I think it's safe enough to skip the compatibility check.
对于 2.0+,我整理了一些示例代码(使用 startForeground)。请注意,现在不推荐使用某些代码,但是,Notification.Builder 使用 API 级别 11 (3.x),这意味着在大多数手机使用兼容的 Android 版本之前我不会使用它。由于绝大多数手机现在都在运行某些版本的 2.x,我认为跳过兼容性检查就足够安全了。
final static int myID = 1234;
//The intent to launch when the user clicks the expanded notification
Intent intent = new Intent(this, SomeActivityToLaunch.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
//This constructor is deprecated. Use Notification.Builder instead
Notification notice = new Notification(R.drawable.icon_image, "Ticker text", System.currentTimeMillis());
//This method is deprecated. Use Notification.Builder instead.
notice.setLatestEventInfo(this, "Title text", "Content text", pendIntent);
notice.flags |= Notification.FLAG_NO_CLEAR;
startForeground(myID, notice);
put this code within onStartCommand()
of your service and you're good to go. (But you could put this section of code anywhere in your service.)
将此代码放在onStartCommand()
您的服务中,您就可以开始了。(但您可以将这部分代码放在您的服务中的任何位置。)
P.S. to stop the service from being in foreground simply use stopForeground(true);
anywhere in your service
PS 停止服务在前台只需stopForeground(true);
在您的服务中的任何地方使用
回答by Abandoned Cart
This code will use the best option available for any API by adding the Android support library to your project. In Eclipse, you would right click the project, go to Android Tools, and click the "Add Support Library..." option to download and add it.
通过将 Android 支持库添加到您的项目,此代码将使用可用于任何 API 的最佳选项。在 Eclipse 中,您可以右键单击该项目,转到 Android 工具,然后单击“添加支持库...”选项以下载并添加它。
final static int myID = 1234;
//The intent to launch when the user clicks the expanded notification
Intent intent = new Intent(this, SomeActivityToLaunch.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0);
if (Integer.parseInt(Build.VERSION.SDK) >= Build.VERSION_CODES.DONUT) {
// Build.VERSION.SDK_INT requires API 4 and would cause issues below API 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setTicker("TICKER").setContentTitle("TITLE").setContentText("CONTENT")
.setWhen(System.currentTimeMillis()).setAutoCancel(false)
.setOngoing(true).setPriority(Notification.PRIORITY_HIGH)
.setContentIntent(pendIntent);
Notification notification = builder.build();
} else {
Notification notice = new Notification(R.drawable.icon_image, "Ticker text", System.currentTimeMillis());
notice.setLatestEventInfo(this, "Title text", "Content text", pendIntent);
}
notification.flags |= Notification.FLAG_NO_CLEAR;
startForeground(myID, notification);
Put the code in the start method you are using for your service, then when you want the foreground process to stop, call stopForeground(true);
anywhere in your service
将代码放在您用于服务的 start 方法中,然后当您希望前台进程停止时,请调用stopForeground(true);
服务中的任何位置
回答by Falmarri
回答by brack
I'm not exactly sure if this is what you're looking for, but all you have to do is create a notification object (mOngoingNote
below) and call startForeground
using a notification ID along with the actual notification.
我不确定这是否是您要查找的内容,但您所要做的就是创建一个通知对象(mOngoingNote
如下所示)并startForeground
使用通知 ID 和实际通知进行调用。
startForeground(NOTEMAN_ID, mOngoingNote);