詳解Android 進(jìn)程
多進(jìn)程
如果需要的時(shí)候,app可以創(chuàng)建多進(jìn)程。
在進(jìn)程里面
各類組件元素的清單文件條目 、 、 和
— 均支持 android:process 屬性,此屬性可以指定該組件應(yīng)在哪個(gè)進(jìn)程運(yùn)行。
默認(rèn)進(jìn)程就是主進(jìn)程。其他進(jìn)程一般來說都是子進(jìn)程。
2個(gè)activity在不同的進(jìn)程里面,可以刷新UI嗎?
<activity android:name=".androidsample.ActivityProgressB"
android:process=":progressb"/>
測試結(jié)果:ActivityProgressB可以正常顯示。這個(gè)其實(shí)很好理解,如果你打開系統(tǒng)相機(jī)頁面,那個(gè)activity肯定與你的app不再一個(gè)進(jìn)程,但是他可以很順利的打開,所以可以支持。
?;?/strong>
OOM_ADJ

這個(gè)就是oom 回kill進(jìn)程的優(yōu)先級(jí)。
進(jìn)程kill的方式
| 場景 | 接口 | 范圍 |
|---|---|---|
| LowMemoryKiller | LowMemoryKiller | 從進(jìn)程的優(yōu)先級(jí)依次kill,釋放內(nèi)存 |
| 三方kill(無root) | killbackgroundprogersss | kill oom_adj>4 |
| 三方kill(有root) | forcestop or kill | 理論上所有,一般是非系統(tǒng)和可見進(jìn)程 |
| 廠商kill功能 | force stop or kill | 理論上所有,包括native |
進(jìn)程保活的目的,就是提供進(jìn)程的優(yōu)先級(jí),降低進(jìn)程被kill的概率。
?;畹奶茁?/strong>
開啟1個(gè)像素的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以后,不允許后臺(tái)進(jìn)程啟動(dòng)后臺(tái)頁面了。也就是想啟動(dòng)一個(gè)前臺(tái)頁面
使用前臺(tái)服務(wù)
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,然后將其取消并取消自己的前臺(tái)顯示
// 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開始以后,禁止后臺(tái)進(jìn)程開啟前臺(tái)進(jìn)程,這個(gè)也是android為了省電考慮的。
多進(jìn)程相互喚醒
這個(gè)就是每個(gè)app,其多個(gè)進(jìn)程,如果比kill掉了,可以通過另一個(gè)喚起。從上面的前臺(tái)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()
//請注意,我們手動(dòng)調(diào)用了jobFinished方法。
//當(dāng)onStartJob返回true的時(shí)候,我們必須手動(dòng)調(diào)用jobFinished方法
//否則該應(yīng)用中的其他job將不會(huì)被執(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 進(jìn)程的詳細(xì)內(nèi)容,更多關(guān)于Android 進(jìn)程的資料請關(guān)注腳本之家其它相關(guān)文章!
- Android 實(shí)現(xiàn)徹底退出自己APP 并殺掉所有相關(guān)的進(jìn)程
- 創(chuàng)建Android守護(hù)進(jìn)程實(shí)例(底層服務(wù))
- Android基于Aidl的跨進(jìn)程間雙向通信管理中心
- Android跨進(jìn)程拋異常的原理的實(shí)現(xiàn)
- Android 雙進(jìn)程守護(hù)的實(shí)現(xiàn)代碼
- android studio3.0.1無法啟動(dòng)Gradle守護(hù)進(jìn)程的解決方法
- Android進(jìn)程間通信實(shí)踐的示例代碼
- 詳解Android跨進(jìn)程IPC通信AIDL機(jī)制原理
- 詳解android webView獨(dú)立進(jìn)程通訊方式
相關(guān)文章
詳談自定義View之GridView單選 金額選擇Layout-ChooseMoneyLayout
下面小編就為大家?guī)硪黄斦勛远xView之GridView單選 金額選擇Layout-ChooseMoneyLayout。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
Android實(shí)現(xiàn)聊天記錄上傳本地服務(wù)器(即時(shí)通訊)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)聊天記錄上傳本地服務(wù)器,即時(shí)通訊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
Android使用BottomNavigationBar實(shí)現(xiàn)底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了Android使用BottomNavigationBar實(shí)現(xiàn)底部導(dǎo)航欄,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Android實(shí)現(xiàn)可折疊式標(biāo)題欄
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)可折疊式標(biāo)題欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Android開發(fā)之OkHttpUtils的具體使用方法
這篇文章主要介紹了Android開發(fā)之OkHttpUtils的具體使用方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-08-08
Android三種方式生成矢量圖之VectorDrawable類使用詳解
這篇文章主要介紹了Android三種方式生成矢量圖的VectorDrawable類,2014年6月26日的I/O?2014開發(fā)者大會(huì)上谷歌正式推出了Android?L,它帶來了全新的設(shè)計(jì)語言Material?Design,新的API也提供了這個(gè)類VectorDrawable2023-02-02
Android程序報(bào)錯(cuò)程序包org.apache.http不存在問題的解決方法
這篇文章主要介紹了Android程序報(bào)錯(cuò)"程序包org.apache.http不存在——Android 6.0已經(jīng)不支持HttpClient" 問題的解決方法,感興趣的小伙伴們可以參考一下2016-06-06

