亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解Android 進程

 更新時間:2020年08月21日 11:52:02   作者:Deman的博客家園  
這篇文章主要介紹了Android 進程的相關資料,幫助大家更好的理解和學習Android開發(fā),感興趣的朋友可以了解下

多進程

如果需要的時候,app可以創(chuàng)建多進程。

在進程里面

各類組件元素的清單文件條目 、 、 和
— 均支持 android:process 屬性,此屬性可以指定該組件應在哪個進程運行。

默認進程就是主進程。其他進程一般來說都是子進程。

2個activity在不同的進程里面,可以刷新UI嗎?

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

測試結果:ActivityProgressB可以正常顯示。這個其實很好理解,如果你打開系統(tǒng)相機頁面,那個activity肯定與你的app不再一個進程,但是他可以很順利的打開,所以可以支持。

?;?/strong>

OOM_ADJ

這個就是oom 回kill進程的優(yōu)先級。

進程kill的方式

場景 接口 范圍
LowMemoryKiller LowMemoryKiller 從進程的優(yōu)先級依次kill,釋放內存
三方kill(無root) killbackgroundprogersss kill oom_adj>4
三方kill(有root) forcestop or kill 理論上所有,一般是非系統(tǒng)和可見進程
廠商kill功能 force stop or kill 理論上所有,包括native

進程?;畹哪康模褪翘峁┻M程的優(yōu)先級,降低進程被kill的概率。

?;畹奶茁?/strong>

開啟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
      //發(fā)送與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將不會被執(zhí)行
      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 進程的資料請關注腳本之家其它相關文章!

相關文章

  • Flutter 移動程序安全性提高的八個建議

    Flutter 移動程序安全性提高的八個建議

    這篇文章主要為大家介紹了Flutter 移動程序安全性提高建議詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Android入門之在子線程中調用Handler詳解

    Android入門之在子線程中調用Handler詳解

    這篇文章主要為大家詳細介紹了Android如何在子線程中調用Handler,文中的示例代碼講解詳細,有需要的朋友可以借鑒參考下,希望能夠對大家有所幫助,
    2022-12-12
  • Android單項綁定MVVM項目模板的方法

    Android單項綁定MVVM項目模板的方法

    這篇文章主要給大家介紹了關于Android單項綁定MVVM項目模板的相關資料,文中通過示例代碼介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-04-04
  • Android 使用ViewPager實現(xiàn)輪播圖效果

    Android 使用ViewPager實現(xiàn)輪播圖效果

    這篇文章主要介紹了Android 使用ViewPager實現(xiàn)輪播圖效果,通過實例代碼給大家講解了適配器和各個方法的作用介紹,需要的朋友可以參考下
    2017-05-05
  • Android Kotlin環(huán)境使用ButterKnife的方法

    Android Kotlin環(huán)境使用ButterKnife的方法

    本篇文章主要介紹了Android Kotlin環(huán)境使用ButterKnife的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Android recyclerview實現(xiàn)縱向虛線時間軸的示例代碼

    Android recyclerview實現(xiàn)縱向虛線時間軸的示例代碼

    本文主要介紹了Android recyclerview實現(xiàn)縱向虛線時間軸的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • Android studio設置指定的簽名文件教程

    Android studio設置指定的簽名文件教程

    這篇文章主要介紹了Android studio設置指定的簽名文件教程,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android自定義RadioGroupX實現(xiàn)多行多列布局

    Android自定義RadioGroupX實現(xiàn)多行多列布局

    這篇文章主要為大家詳細介紹了Android自定義RadioGroupX實現(xiàn)多行多列布局,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android實現(xiàn)退出時關閉所有Activity的方法

    Android實現(xiàn)退出時關閉所有Activity的方法

    這篇文章主要介紹了Android實現(xiàn)退出時關閉所有Activity的方法,主要通過自定義類CloseActivityClass實現(xiàn)這一功能,需要的朋友可以參考下
    2014-09-09
  • React Native學習之Android的返回鍵BackAndroid詳解

    React Native學習之Android的返回鍵BackAndroid詳解

    這篇文章主要給大家介紹了關于React Native學習之Android的返回鍵BackAndroid的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用React Native具有一定的參考學習價值,需要的朋友們下面隨著小編來一起看看吧。
    2017-10-10

最新評論