亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

詳解Android 進程

瀏覽:79日期:2022-09-22 18:41:25

多進程

如果需要的時候,app可以創建多進程。

在進程里面

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

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

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

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

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

保活

OOM_ADJ

詳解Android 進程

這個就是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.androidsampleimport android.annotation.TargetApiimport android.app.Notificationimport android.app.NotificationChannelimport android.app.NotificationManagerimport android.app.Serviceimport android.content.Contextimport android.content.Intentimport android.os.Buildimport android.os.Handlerimport android.os.IBinderimport androidx.core.app.NotificationCompatimport com.demanmath.androidms.AppLogimport 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 NotificationManagermanager.cancel(NOTIFICATION_ID)stopSelf() }, 100) } }}

但是androidQ開始以后,禁止后臺進程開啟前臺進程,這個也是android為了省電考慮的。

多進程相互喚醒

這個就是每個app,其多個進程,如果比kill掉了,可以通過另一個喚起。從上面的前臺service的功效有些類似。

同樣的問題,android Q以后無效。

JobSchedule

package com.demanmath.androidms.jobserviceimport android.app.job.JobParametersimport android.app.job.JobServiceimport android.content.Intentimport android.os.Handlerimport android.os.Messageimport android.widget.Toastimport 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.jobserviceimport android.app.job.JobInfoimport android.app.job.JobSchedulerimport android.content.ComponentNameimport android.content.Contextimport 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
相關文章:
主站蜘蛛池模板: 日韩一区二区三区四区区区 | 91福利精品老师国产自产在线 | 好吊色青青青国产在线观看 | 国产区亚洲区 | 在线免费观看一区二区三区 | 亚洲一级黄色 | 99黄色网 | 伊人久久成人成综合网222 | 可以看黄色的网址 | 色拍拍在线精品视频在线观看 | 青青久在线视频免费观看 | 南京巨根无套操到你窒息 | 国产亚洲一区二区在线观看 | 久久国产精品亚洲综合 | 我要看黄色录像一级片 | 国产美女精品在线观看 | 美国黄色一级大片 | 国产福利专区 | 韩国美女激情视频一区二区 | 劲爆欧美色欧美 | 免费国产成人手机在线观看 | 国产97色在线中文 | 你懂的网站在线观看网址 | 精品欧美一区二区vr在线观看 | 曰本人一级毛片免费完整视频 | 国产成人精品视频一区二区不卡 | 国产xxxx做受欧美88xx00tube | 久久青草国产精品一区 | 深夜福利1000 | 最新亚洲 | 精品国产欧美一区二区五十路 | 人妖欧美一区二区三区四区 | 欧美性猛交xxxx免费看蜜桃 | 国产馆在线观看视频 | 亚洲精品一区二区观看 | 久久aa毛片免费播放嗯啊 | 亚洲香蕉毛片久久网站老妇人 | 国产一区二区三区在线观看免费 | 免费国产成人高清视频网站 | 黄色高清视频在线观看 | 国产区成人综合色在线 |