判断当前进程是否为主进程
判断当前进程是否为主线程
package song.cycle.life.com.androidlifecycle;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Looper;
import android.util.Log;
/**
* AndroidLifeCycle
* Created by peter
* on 2018.03
*/
public class LifeApplication extends Application {
/**
* 是否为主线程
*/
private boolean isMainThread = Looper.getMainLooper() == Looper.myLooper();
/**
* 是否为主进程
*/
private boolean isMainProcess ;
@Override
public void onCreate() {
super.onCreate();
isMainProcess = getApplicationContext().getPackageName().equals
(getCurrentProcessName());
Log.e("song", "是否为主线程" + isMainThread);
Log.e("song", "是否为主进程" + isMainProcess);
}
/**
* 获取当前进程名
*/
private String getCurrentProcessName() {
int pid = android.os.Process.myPid();
String processName = "";
ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService
(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo process : manager.getRunningAppProcesses()) {
if (process.pid == pid) {
processName = process.processName;
}
}
return processName;
}
}