详解Android 进程

 更新时间:2020年08月21日 11:52:02   作者:Deman的博客家园  
这篇文章主要介绍了Android 进程的相关资料,帮助大家更好的理解和学习Android开发,感兴趣的朋友可以了解下

多进程

如果需要的时候,app可以创建多进程。

在进程里面

各类组件元素的清单文件条目 、 、 和
— 均支持 android:process 属性,此属性可以指定该组件应在哪个进程运行。

默认进程就是主进程。其他进程一般来说都是子进程。

2个activity在不同的进程里面,可以刷新UI吗?

<activity android:name=".androidsample.ActivityProgressB"
      android:process=":progressb"/>

测试结果:ActivityProgressB可以正常显示。这个其实很好理解,如果你打开系统相机页面,那个activity肯定与你的app不再一个进程,但是他可以很顺利的打开,所以可以支持。

保活

OOM_ADJ

这个就是oom 回kill进程的优先级。

进程kill的方式

场景 接口 范围
LowMemoryKiller LowMemoryKiller 从进程的优先级依次kill,释放内存
三方kill(无root) killbackgroundprogersss kill oom_adj>4
三方kill(有root) forcestop or kill 理论上所有,一般是非系统和可见进程
厂商kill功能 force stop or kill 理论上所有,包括native

进程保活的目的,就是提供进程的优先级,降低进程被kill的概率。

保活的套路

开启1个像素的activity

2020-08-14 14:29:48.630 1164-8504/system_process W/ActivityTaskManager: Background activity start [callingPackage: com.demanmath.androidms; callingUid: 10398; isCallingUidForeground: false; isCallingUidPersistentSystemProcess: false; realCallingUid: 10398; isRealCallingUidForeground: false; isRealCallingUidPersistentSystemProcess: false; originatingPendingIntent: null; isBgStartWhitelisted: false; intent: Intent { flg=0x10000000 cmp=com.demanmath.androidms/.androidsample.LiveActivity }; callerApp: ProcessRecord{a168b71 2429:com.demanmath.androidms/u0a398}]

在android Q以后,不允许后台进程启动后台页面了。也就是想启动一个前台页面

使用前台服务

package com.demanmath.androidms.androidsample

import android.annotation.TargetApi
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Handler
import android.os.IBinder
import androidx.core.app.NotificationCompat
import com.demanmath.androidms.AppLog
import com.demanmath.androidms.R

/**
 *  @author   DemanMath
 *  @date    2020/8/14
 *
 */
class KeepLiveService:Service() {
  val NOTIFICATION_ID = 0x11
  val NOTIFICATION_CHANNEL_ID = "demanmathId"
  val channelName = "My Background Service"

  companion object {
    const val NOTIFICATION_ID = 0x11
  }
  override fun onBind(intent: Intent?): IBinder? {
    return null
  }

  override fun onCreate() {
    super.onCreate()
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
      startForeground(NOTIFICATION_ID, Notification())
    } else {
      startMyOwnForeground()
      startService(Intent(this, InnerService::class.java))
    }
  }

  @TargetApi(value = Build.VERSION_CODES.O)
  private fun startMyOwnForeground() {
    AppLog.d()
    val chan = NotificationChannel(
      NOTIFICATION_CHANNEL_ID,
      channelName,
      NotificationManager.IMPORTANCE_NONE
    )
    chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
    val manager =
      (getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager)
    manager.createNotificationChannel(chan)
    val notificationBuilder =
      NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
    val notification = notificationBuilder.setOngoing(true)
      .setSmallIcon(R.drawable.ic_launcher_background)
      .setContentTitle("App is running in background")
      .setPriority(NotificationManager.IMPORTANCE_MIN)
      .setCategory(Notification.CATEGORY_SERVICE)
      .build()
    startForeground(NOTIFICATION_ID, notification)
  }

  class InnerService : Service() {
    override fun onBind(intent: Intent): IBinder? {
      return null
    }

    override fun onCreate() {
      super.onCreate()
      //使用channeId & channelName
      //发送与KeepLiveService中ID相同的Notification,然后将其取消并取消自己的前台显示
//      val builder: Notification.Builder = Notification.Builder(this)
//      builder.setSmallIcon(R.mipmap.ic_launcher)
//      startForeground(NOTIFICATION_ID, builder.build())
      Handler().postDelayed(Runnable {
        stopForeground(true)
        val manager =
          getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        manager.cancel(NOTIFICATION_ID)
        stopSelf()
      }, 100)
    }
  }

}

但是androidQ开始以后,禁止后台进程开启前台进程,这个也是android为了省电考虑的。

多进程相互唤醒

这个就是每个app,其多个进程,如果比kill掉了,可以通过另一个唤起。从上面的前台service的功效有些类似。

同样的问题,android Q以后无效。

JobSchedule

package com.demanmath.androidms.jobservice

import android.app.job.JobParameters
import android.app.job.JobService
import android.content.Intent
import android.os.Handler
import android.os.Message
import android.widget.Toast
import com.demanmath.androidms.AppLog

/**
 *  @author   DemanMath
 *  @date    2020/8/20
 *
 */
class JobDemoService:JobService() {

  override fun onCreate() {
    super.onCreate()
    AppLog.i()
  }

  override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    AppLog.i()
    return super.onStartCommand(intent, flags, startId)
  }

  private var mHandler = object:Handler(){
    override fun handleMessage(msg: Message) {
      AppLog.i()
      Toast.makeText(
        applicationContext,
        "JobService task running", Toast.LENGTH_SHORT
      ).show()
      //请注意,我们手动调用了jobFinished方法。
      //当onStartJob返回true的时候,我们必须手动调用jobFinished方法
      //否则该应用中的其他job将不会被执行
      jobFinished(msg.obj as JobParameters, false)
    }
  }
  override fun onStartJob(params: JobParameters?): Boolean {
    AppLog.i()
    mHandler.sendMessage(Message.obtain(mHandler,1,params))
    return true
  }

  override fun onStopJob(params: JobParameters?): Boolean {
    AppLog.i()
    mHandler.removeMessages(1)
    return false
  }

}
package com.demanmath.androidms.jobservice

import android.app.job.JobInfo
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import com.demanmath.androidms.AppLog

/**
 *  @author   DemanMath
 *  @date    2020/8/20
 *
 */
class JobHelper(var context: Context) {

  lateinit var jobScheduler:JobScheduler

  fun startJob(){
    AppLog.i()
    jobScheduler = context.getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
    var builder = JobInfo.Builder(1, ComponentName(context.packageName,JobDemoService::class.java.name))

//    builder.setBackoffCriteria(1000L,JobInfo.BACKOFF_POLICY_LINEAR)
    var boolean = jobScheduler.schedule(builder.build())
    AppLog.i(boolean.toString())
  }
}

以上就是详解Android 进程的详细内容,更多关于Android 进程的资料请关注脚本之家其它相关文章!

相关文章

  • 浅谈一下Android的Activity

    浅谈一下Android的Activity

    这篇文章主要介绍了浅谈一下Android的Activity,活动是所有安卓应用程序的门面,凡事你在应用中看到的东西,都是放到活动中的,需要的朋友可以参考下
    2023-04-04
  • android传送照片到FTP服务器的实现代码

    android传送照片到FTP服务器的实现代码

    这篇文章主要为大家详细介绍了android传送照片到FTP服务器的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • android apk反编译到java源码的实现方法

    android apk反编译到java源码的实现方法

    Android由于其代码是放在dalvik虚拟机上的托管代码,所以能够很容易的将其反编译为我们可以识别的代码,本文将详细介绍,需要的朋友可以参考下
    2012-12-12
  • Android开启新线程播放背景音乐

    Android开启新线程播放背景音乐

    这篇文章主要为大家详细介绍了Android开启新线程播放背景音乐,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-12-12
  • Android自定义View实现角度选择器

    Android自定义View实现角度选择器

    前几天在Google Photos查看照片,用了一下它的图片剪裁功能,于是我马上就被其界面和操作吸引。后来想模仿做一个和Google Photos裁图页面几乎一模一样的角度选择器,本文比较基础,在阅读本文前只需要掌握最基础的自定义View知识和Android事件知识。下面来一起学习下吧。
    2016-11-11
  • Android从图片获取二维码的方法

    Android从图片获取二维码的方法

    这篇文章主要为大家详细介绍了Android从图片获取二维码的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • Android程序开发之UIScrollerView里有两个tableView

    Android程序开发之UIScrollerView里有两个tableView

    这篇文章主要介绍了UIScrollerView里有两个tableView 的相关资料,需要的朋友可以参考下
    2016-04-04
  • Android使用CircleImageView实现圆形头像的方法

    Android使用CircleImageView实现圆形头像的方法

    圆形头像看起来非常美观,下文通过实例代码给大家介绍android中使用CircleImageView实现圆形头像的方法,一起看看吧
    2016-09-09
  • Flutter持久化存储之数据库存储(sqflite)详解

    Flutter持久化存储之数据库存储(sqflite)详解

    这篇文章主要给大家介绍了关于Flutter持久化存储之数据库存储的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用Flutter具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2019-03-03
  • Android实现静默安装实例代码

    Android实现静默安装实例代码

    本篇文章主要介绍了Android实现静默安装实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06

最新评论