怎样提高线程优先级notification的优先级

Notification在手机的运用中是很常见的,比如我们收到一个短信,在我们的通知栏就会显示一个消息的图标用来提示我们,这种我们就可以用Notification来实现。他有很多的用法,比如类似消息的一个提示,进度条式的提示,折叠式啊,或者悬挂式等。下面我们可以看一个简单的也是最基本的Notification:
第一种:基本的Notification
& 1.API 11 以下的,现在被弃用了,它的简单用法是这样的
& &1.1这里需要使用PendingIntent,跟Intent相似,Intent 是及时启动,intent 随所在的activity 消失而消失,而PendingIntent它不是马上被调用,它主要用于即将发生的事情,在Notification中,它在下拉状态条点击时候才会发生activity的跳转
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
& 1.2然后我们在来看Notification 的使用方法
Notification notify = new Notification();
notify.icon = R.drawable.ic_
notify.tickerText = "有新短消息了!";
notify.when = System.currentTimeMillis();
notify.setLatestEventInfo(this, "通知的消息title",
在后来的版本中基本上会提示找不到,或者提示不建议使用
"消息内容", pendingIntent);
notify.number = 1;
//消息的数量
notify.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。
&简单提一下2.x版本的处理方式:
Notification notify1 = new Notification(R.drawable.message, "有新短消息了", System.currentTimeMillis());
& 1.3然后我们需要使用系统的通知管理器来发起通知&
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_FLAG, notify);
//发起通知
我们看一下效果图:是不是很熟悉的感觉啊
& 2.API11以后 主要是通过Notification.Builder来创建通知,我们看一下代码实现
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);Notification notify= new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏中的小图片,尺寸一般建议在24&24, 这里也可以设置大图标
.setTicker(有新短消息了!")// 设置显示的提示文字
.setContentTitle("Title")// 设置显示的标题
.setContentText("This is message content")// 消息的详细内容
.setContentIntent(pendingIntent) // 关联PendingIntent
.setNumber(1) // 在TextView的右方显示的数字,可以在外部定义一个变量,点击累加setNumber(count),这时显示的和
.getNotification(); // 需要注意build()是在API level16及之后增加的,在API11中可以使用getNotificatin()来代替
notify.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager manager =(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_FLAG, notify);
总体上来是没有什么变化,同时我们还可以设置下面的一些属性:
//设置优先级
notify.setPriority(Notification.PRIORITY_DEFAULT);
//设置通知类别
notify.setCategory(Notification.CATEGORY_MESSAGE);
3.我们也可以使用自定义的Notification:主要使用RemoteViews,
第一步:写好你想要&&Notification样式的xml 文件第二步:在上面的列子中加入下面的代码:
RemoteViews remote = new RemoteViews(getPackageName(),
R.layout.my_notification);
rv.setTextViewText(R.id.content, "hello!");
myNotify.contentView =
可以使用它做折叠视图,简单说一下,它主要是用来显示长文本,它拥有2个视图,一个是普通状态下的,一个是展开状态下的,
Intent intent = new Intent(Intent.ACTION_MAIN);
PendingIntent contentIntent = PendingIntent.getActivity(this, 1,
intent, 0);
Notification.Builder buider = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("您有新短消息")
.setContentTitle("Notification Title")
.setContentText("This is message")
.setContentIntent(contentIntent)
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.my_notification);
contentView.setTextViewText(R.id.text_content, "show!");
Notification notification1=buider.build();
//指定视图Notification 正常状态下的视图
notification1.contentView=contentV
RemoteViews expandView = new RemoteViews(getPackageName(),
R.layout.my_notification_expand);
//展开时的视图
notification1.bigContentView = expandV
manager.notify(NOTIFICATION_FLAG, notification1);
4.悬挂式Notification,他是5.0中新增的,也就是API中的Headsup的Notification,可以子啊不打断用户操作的时候,给用户通知
&它跟其他的不同,主要在下面这句代码:
//设置为悬挂,主要设置 setFullScreenIntent
notify.setContentText("Heads-Up,Notification on5.0").setFullScreenIntent(pendingIntent, true);
&5.进度条的Notification,我们一般常见的是下载软件的时候,显示的会有一个下载进度的通知,其实这个也是很好实现的&
&在Api中是这样介绍的:
To use a progress indicator on platforms starting with Android 4.0, call&. For previous versions, you must create your own custom notification layout that includes a&&view.
&它的意思是在android4.0以上平台,我们可以直接setProgress()的方法,但是对于之前的我们只能自定义布局。
&我们来简单看一下:这个是api给的一个demo。下面我们重点看一下着色的部分
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
builder = new NotificationCompat.Builder(this);
builder.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setSmallIcon(R.mipmap.ic_launcher);
new Thread(
new Runnable() {
public void run() {
// Do the "lengthy" operation 20 times
for (incr = 0; incr &= 100; incr+=5) {
// Sets the progress indicator to a max value, the
// current completion percentage, and "determinate"
builder.setProgress(100, incr, false);
// Displays the progress bar for the first time.
mNotificationManager.notify(0, builder.build());
// Sleeps the thread, simulating an operation
// that takes time
// Sleep for 5 seconds
Thread.sleep(5*1000);
} catch (InterruptedException e) {
Log.d(TAG, "sleep failure");
// When the loop is finished, updates the notification
builder.setContentText("Download complete")
// Removes the progress bar
.setProgress(0,0,true);
mNotificationManager.notify(NOTIFICATION_FLAG, builder.build());
// Starts the thread by calling the run() method in its Runnable
).start();
mBuilder.setProgress(100, incr, false);和 mBuilder.setProgress(0, 0, true);的区别,如果把蓝色的部分替换为前者,在运行的时候,我们会看见是它下载完成后会给我们一个通知说下载完成,但是后者会给我们提示下载完成的同时,进度条是在运行的,不断的和滚动,也就是效果是有所不同如下图:第一个图是下载在进行的时候,第二个是我使用后 mBuilder.setProgress(0, 0, true),下载完成的时候,我们可以发现他还存在进度条,但是一直在滚动,我这里是静态图片,大家可以运行一下看看效果,第三个图,mBuilder.setProgress(100,incr,false) ,下载完成时显示的通知
还有一些其他的,辟比如锁屏下的,显示等级的Notification,在这里我就不写了,大家可以没事看看api。介绍的很详细。
阅读(...) 评论()本博客仅仅要没有注明“转”。那么均为原创,转贴请注明本博客链接链接基本上大家都知道提高service优先级能够在非常大程度上让你的service免于由于内存不足而被kill,当然系统仅仅是在此时先把优先级低的kill掉。假设内存还是不够,也会把你的service干掉的。只是如今的机器不像几年前了,基本上不会发生那种情况。先来看看网上常见的错误方法:1.android:persistent=&true&对第三方app无效,以下是官方说明android:persistentWhether or not the application should remain running at all times — &true& if it should, and &false& if not. The default value is &false&. Applications should not no persistence mode is intended only for certain system applications.&2.onDestroy中重新启动serviceservice被系统杀死的时候并不一定会运行onDestroy。拿什么重新启动&3.android:priorityservice根本没有这属性&4.setForeground这个是有效的,可是网上的样例却都是无效的原因是參数错误&让service免于非难的办法是提高它的重要性,在官方文档中已经说明进程有五个级别,当中前台进程最重要。所以最后被杀死。怎样使之变成前台进程能够參阅官方文档。&这里仅仅说怎样使用setForeground将service设置为前台进程Notification notification = new Notification();
notification.flags = Notification.FLAG_ONGOING_EVENT;
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
service.startForeground(1, notification);上面的三个属性放到一起,值为0x62。
* Bit to be bitwise-ored into the {@link #flags} field that should be
* set if this notification is in reference to something that is ongoing,
* like a phone call.
It should not be set if this notification is in
* reference to something that happened at a particular point in time,
* like a missed phone call.
public static final int FLAG_ONGOING_EVENT
* Bit to be bitwise-ored into the {@link #flags} field that should be
* set if the notification should not be canceled when the user clicks
* the Clear all button.
public static final int FLAG_NO_CLEAR
* Bit to be bitwise-ored into the {@link #flags} field that should be
* set if this notification represents a currently running service.
* will normally be set for you by {@link Service#startForeground}.
public static final int FLAG_FOREGROUND_SERVICE = 0x;最后。我们能够使用以下命令看看手机中的哪些应用这么干了,你在平时使用的时候是不是他们存活时间最长。最不easy被系统干掉dumpsys notification转贴请保留以下链接本人blog地址
阅读(...) 评论()3230人阅读
Android牛扎糖(1)
Android7.0新特性
上篇讲了,这篇讲讲Notification新增的API。首先奉上官网介绍——。
2 通知增强功能
注:本文引用v4 支持库中的 NotificationCompat.Builder 类。Android 3.0(API 级别 11)中已添加类 Notification.Builder。
2.1 通知优先级
您可以根据需要设置通知的优先级。优先级充当一个提示,提醒设备 UI 应该如何显示通知。 要设置通知的优先级,请调用 NotificationCompat.Builder.setPriority() 并传入一个 NotificationCompat 优先级常量。有五个优先级别,范围从 PRIORITY_MIN (-2) 到 PRIORITY_MAX (2);如果未设置,则优先级默认为 PRIORITY_DEFAULT (0)。
2.2 创建简单通知
以下代码段说明了一个指定某项 Activity 在用户点击通知时打开的简单通知。该代码将创建 TaskStackBuilder 对象并使用它来为操作创建 PendingIntent。部分对此模式做了更详尽的阐述:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class)
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this)
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class)
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent)
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
PendingIntent.FLAG_UPDATE_CURRENT
mBuilder.setContentIntent(resultPendingIntent)
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build())
就这么简单。您的用户现已收到通知。
2.2 将扩展布局应用于通知(仅Android4.1及以上[ &= API16])
要使通知出现在展开视图中,请先创建一个带有所需普通视图选项的
对象。接下来,调用以扩展布局对象作为其参数的 。
以下代码段演示了如何更改在前面的代码段中创建的通知,以便使用扩展布局:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Event tracker")
.setContentText("Events received")
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = new String[6];
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");
// Moves events into the expanded layout
for (int i=0; i & events. i++) {
inboxStyle.addLine(events[i]);
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inBoxStyle);
// Issue the notification here.
处理兼容性
并非所有通知功能都可用于某特定版本,即便用于设置这些功能的方法位于支持库类
中也是如此。 例如,依赖于扩展通知的操作按钮仅会显示在 Android 4.1 及更高版本的系统中,这是因为扩展通知本身仅在 Android 4.1 及更高版本的系统中可用。
为了确保最佳兼容性,请使用
及其子类(特别是 )创建通知。此外,在实现通知时,请遵循以下流程:
为所有用户提供通知的全部功能,无论他们使用何种版本的 Android 系统。 为此,请验证是否可从应用的
中获得所有功能。要执行此操作,您可能需要添加新的 Activity。
例如,若要使用
提供停止和启动媒体播放的控件,请先在应用的
中实现此控件。
确保所有用户均可通过点击通知启动
来获得该中的功能。 为此,请为
创建 。调用
添加到通知。
现在,将要使用的扩展通知功能添加到通知。请记住,您添加的任何功能还必须在用户点击通知时启动的
2.3 管理通知
当您需要为同一类型的事件多次发出同一通知时,应避免创建全新的通知, 而是应考虑通过更改之前通知的某些值和/或为其添加某些值来更新通知。
例如,Gmail 通过增加未读消息计数并将每封电子邮件的摘要添加到通知,通知用户收到了新的电子邮件。 这称为“堆叠”通知;设计指南对此进行了更详尽的描述。
注:此 Gmail 功能需要“收件箱”扩展布局,该布局是自 Android 4.1 版本起可用的扩展通知功能的一部分。
2.3.1 更新通知
要将通知设置为能够更新,请通过调用 NotificationManager.notify() 发出带有通知 ID 的通知。 要在发出之后更新此通知,请更新或创建 NotificationCompat.Builder 对象,从该对象构建 Notification 对象,并发出与之前所用 ID 相同的 Notification。如果之前的通知仍然可见,则系统会根据 Notification 对象的内容更新该通知。相反,如果之前的通知已被清除,系统则会创建一个新通知。
以下代码段演示了经过更新以反映所发生事件数量的通知。 它将通知堆叠并显示摘要:
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
mNotifyBuilder.setContentText(currentText)
.setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(
mNotifyBuilder.build());
2.3.2 删除通知
除非发生以下情况之一,否则通知仍然可见:
- 用户单独或通过使用“全部清除”清除了该通知(如果通知可以清除)。
- 用户点击通知,且您在创建通知时调用了 setAutoCancel()。
- 您针对特定的通知 ID 调用了 cancel()。此方法还会删除当前通知。
- 您调用了 cancelAll() 方法,该方法将删除之前发出的所有通知。
2.4 在通知中显示进度
要在 Android 4.0 及更高版本的平台上使用进度指示器,需调用 。对于早期版本,您必须创建包括
视图的自定义通知布局。
下文介绍如何使用 setProgress() 在通知中显示进度。
显示持续时间固定的进度指示器
要显示限定形式的进度栏,请通过调用
将进度栏添加到通知,然后发出通知。随着操作继续进行,递增 progress 并更新通知。操作结束时, progress 应该等于 max。调用
的常见方法是将 max 设置为 100,然后将 progress 作为操作的“完成百分比”值递增。
您可以在操作完成后仍保留显示进度栏,也可以将其删除。无论哪种情况,都请记住更新通知文本以显示操作已完成。 要删除进度栏,请调用 。例如:
mNotifyManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("Picture Download")
.setContentText("Download in progress")
.setSmallIcon(R.drawable.ic_notification);
new Thread(
new Runnable() {
public void run() {
for (incr = 0; incr &= 100; incr+=5) {
mBuilder.setProgress(100, incr, false);
mNotifyManager.notify(0, mBuilder.build());
Thread.sleep(5*1000);
} catch (InterruptedException e) {
Log.d(TAG, "sleep failure");
mBuilder.setContentText("Download complete")
.setProgress(0,0,false);
mNotifyManager.notify(ID, mBuilder.build());
).start();
2.5 锁定屏幕通知
随着 Android 5.0(API 级别 21)的发布,通知现在还可显示在锁定屏幕上。您的应用可以使用此功能提供媒体播放控件以及其他常用操作。 用户可以通过“设置”选择是否将通知显示在锁定屏幕上,并且您可以指定您应用中的通知在锁定屏幕上是否可见。
设置可见性
您的应用可以控制在安全锁定屏幕上显示的通知中可见的详细级别。 调用 setVisibility() 并指定以下值之一:
显示通知的完整内容。
不会在锁定屏幕上显示此通知的任何部分。
显示通知图标和内容标题等基本信息,但是隐藏通知的完整内容。
后,您还可以提供其中隐藏了某些详细信息的替换版本通知内容。例如,短信 应用可能会显示一条通知,指出“您有 3 条新短信”,但是隐藏了短信内容和发件人。要提供此替换版本的通知,请先使用 NotificationCompat.Builder 创建替换通知。创建专用通知对象时,请通过 setPublicVersion() 方法为其附加替换通知。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:65728次
积分:1547
积分:1547
排名:千里之外
原创:86篇
评论:17条
(1)(5)(1)(1)(2)(1)(2)(4)(8)(39)(11)(8)(2)(5)(2)(1)(1)(1)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'2782人阅读
android开发(46)
提高进程优先级
&&&&&&& startForeground(1, new Notification());
降低进程优先级
&&&&&&& stopForeground(true);
onStart()&方法中进行提高优先级操作,然后在onStop()方法中进行降低优先级操作
&这个方法可以将对应AP的ADJ临时提高到2。
找到这个进程对应的AndroidMannifest.xml文件,在其中添加属性『android:persistent=&true&』,
这样可以将该进程设置为常驻内存进程,就可以降低被Kill的概率。以Acore进程为例,
在&/package/providers/ContactsProvider/AndroidMannifest.xml&文件中增加一行『android:persistent=&true&』
具体修改示例如下:
&& &application android:process=&android.process.acore&
&&&&&&&&&&&& android:label=&@string/app_label&
&&&&&&&&&&&& android:icon=&@drawable/app_icon&
&&&&&&&&&&&& android:allowBackup=&false&
&&&&&&&&&&&&&android:persistent=&true&&&!--新增加代码,保证acore进程不被ActivityManager杀死--&
在需要提高优先级的地方调用:&
&&&&&&& final Intent it = new Intent();&
&&&&&&& it.setAction(&android.intent.action.BOOST_DOWNLOADING&);&
&&&&&&& it.putExtra(&package_name&, &com.android.contacts&);&
&&&&&&& it.putExtra(&enabled&,&true);&
&&&&&&& context.sendBroadcast(it);&
在需要恢复到正常优先级时调用:&
&&&&&&& final Intent it = new Intent();&
&&&&&&& it.setAction(&android.intent.action.BOOST_DOWNLOADING&);&
&&&&&&& it.putExtra(&package_name&, &com.android.contacts&);&
&&&&&&& it.putExtra(&enabled&,&false);&
&&&&&&& context.sendBroadcast(it);
onStart()&方法中进行提高优先级操作,然后在onStop()方法中进行降低优先级操作
这个方法可以将对应AP的ADJ临时提高到7。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:114845次
积分:1900
积分:1900
排名:千里之外
原创:71篇
转载:40篇
评论:11条
(2)(3)(1)(2)(3)(1)(13)(4)(1)(4)(7)(3)(2)(8)(1)(5)(3)(25)(14)(2)(5)(2)
(window.slotbydup = window.slotbydup || []).push({
id: '4740881',
container: s,
size: '200,200',
display: 'inlay-fix'如何避免Service被系统杀死,随便在网上搜一下,都能搜到好几种方法,但是每一种方法都有不同的适用环境。
1. 添加android:persistent=&true&
添加android:persistent=&true&到AndroidManifest.xml,Google文档描述如下:
Whether or not the application should remain running at all times-true&&if it should, and&&false&if not. The default value is&&false&.Applications should not no persistence
mode is intended only for certain system applications.可见这个属性不能随便用,到目前为止,我所发现使用该属性的应用只有Phone,而且使用是要求权限的,所以这个属性对第三方应用来说意义不是很大;
2. 设置onStartCommand()的返回值
这个思路比较有用,我们着重分析一下,该方法有四种返回值:
START_STICKY
START_NOT_STICKY
START_REDELIVER_INTENT
START_STICKY_COMPATIBILITY
那么简单的说,四种模式的区别如下:
START_STICKY:kill后会被重启,但是重启后调用onStarfCommand()传进来的Intent参数为null,说明被kill的时候没有保存Intent;
START_STICKY_COMPATIBILITY:START_STICKY的兼容版,但是不能保证onStartCommand()方法被调用,如果应用程序的targetSdkVersion 小于 2.0版本,就会返回该值,否则返回START_STICKY,同时再次启动时只会调用onCreate(),不保证能调用onStartCommand()方法
START_NOT_STICKY:kill之后不会被重启;
START_REDELIVER_INTENT:kill后会被重启,同时重启调用onStartCommand()时再次传入保存的Intent。
启动一个service,然后在recent app里面杀死该进程,使用不同返回值时的log如下:
START_REDELIVER_INTENT
同上,不过传入的Intent为null,同时startId发生了变化,startId的官方解释是“A unique integer representing this specific request to start. Use with stopSelfResult(int)”,也就是说重启和第一次启动不是同一个request,也可以认为这是一个全新的request;
START_STICKY_COMPATIBILITY
这次重启根本就没有调用onStartCommand()方法;
START_NOT_STICKY
没有再次启动被杀掉的service。
测试的代码很简单,大家可以自己尝试。现在有一个问题:我们该如何判断启动的service是正常启动还是杀死后被重启的,因为有时候我们需要知道这些信息
原理:service所在的activity和running task栈顶的activity做比较,因为一旦service所在的activity被杀死,那么系统会跳转到其他应用,如比桌面,或者SystemUI,或者用户可以打开的task栈中的其他TOP activity,此时的running task栈顶的activity肯定不是被杀死的activity了。
可以发现,虽然安排了启动,但是很快就被Force Stop了,这样也就失去了被重启的机会,至于在Settings中杀死进程的原理,有机会咱们展开讲。
3. startForeground()提高service的进程等级
我们知道Android进程分为5个等级:foreground process,&visible process,&Service process,&background process,&empty process,当系统资源吃紧的时候,会按照进程等级从低到高的顺序,同时根据进程消耗的资源从多到少的原则来kill一些进程,而service正处于第三个等级,如果能够提高service所在进程的等级,那么它被杀死的概率就会小一些。
可以利用Service的startForeground()方法将Service的进程等级从第三级提升到第一级foreground process。
至于使用嘛,可以在在onCreate()或者onStartComman()方法中调用,然后可以在onDestroy()或者其他地方调用stopForeground(boolean removeNotification)方法来stop。
关于进程等级可访问:
当然啦,网上还有一些其他的避免Service被杀死或者kill后重启的方法,比如监听android.intent.action.USER_PRESENT,来启动service,或者提高service IntentFilter的priority等,都能算是一些在某些特殊情况下可以其作用的方法,倒也不妨尝试一下。
还有人说用AlarmManager
监听系统启动的broadcast,然后每10秒一个周期,不停的发广播,这就是说应用一旦启动,就会不断的发广播,个人觉得这种方式不靠谱,原因如下:
1. 这样做无谓的操作,会消耗系统资源;
2. 一旦APP进程被杀死,怎么保证你的receiver不被杀死?
3. 不停的启动service,加入service中启动了其他的线程在做耗时的操作,这样做会产生大量的线程做重复的操作,即便service中没有启动其他线程,不断的调用onStartCommand()方法都不算是一个好办法。
当然了,如果实在没办法,必须得使用这种solution的话,我们可以判断service是否是alive,至于方法百度一下就有了。
至于有人说在onDestroy()中重启service,上面打出来的log大家也看到了,被kill的时候都没机会去调用onDestroy()。
本文已收录于以下专栏:
相关文章推荐
本文出自:/thread-292-1-1.html
欢迎转载,转载请注明出自:    安卓开发网
    笔者...
Android面试题-service被kill之后怎么让它重启
win7安装tomcat后重启电脑出现“unable to open the service tomcat6”(2)[申明:来源于网络]
win7安装tomcat后重启电脑出现“unable to ...
应用程序如何避免内存泄漏以
及如何检查泄漏原因
的应用程序开发使用的
机制使得在堆上分
问题:在java层写入uboot变量保存输出模式后,需要重启才能重新初始化和设置一些驱动。之前的做法是发送reset消息,让系统重启。但是这样做比较耗时。
方法:在java层调用root权限的命令,...
他的最新文章
讲师:王哲涵
讲师:韦玮
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)}

我要回帖

更多关于 提高优先级 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信