android判断某服务是否正在运行
答案:2 悬赏:20 手机版
解决时间 2021-04-23 12:59
- 提问者网友:溺爱和你
- 2021-04-22 12:21
android判断某服务是否正在运行
最佳答案
- 五星知识达人网友:你可爱的野爹
- 2021-04-22 13:59
Android系统中,判断应用服务是否启动有两种方式:
1.根据包名判断,以下为判断代码:
public boolean checkApkExist(Context context, String packageName) {
if (packageName == null || “”.equals(packageName))
return false;
try {
ApplicationInfo info = context.getPackageManager()
.getApplicationInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
2. 根据Intent判断,以下为判断代码:
public boolean checkApkExist(Context context, Intent intent) {
List list = context.getPackageManager().queryIntentActivities(intent, 0);
if(list.size() > 0){
return true;
}
return false;
}
public boolean checkApkExist(Context context, String packageName) {
if (packageName == null || “”.equals(packageName)) return false;
try {
ApplicationInfo info = context.getPackageManager() .getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES); return true;
} catch (NameNotFoundException e) { return false; }
}
public boolean checkApkExist(Context context, Intent intent) {
List list = context.getPackageManager()
.queryIntentActivities(intent, 0);
if (list.size() > 0) {
return true;
}
return false;
}
1.根据包名判断,以下为判断代码:
public boolean checkApkExist(Context context, String packageName) {
if (packageName == null || “”.equals(packageName))
return false;
try {
ApplicationInfo info = context.getPackageManager()
.getApplicationInfo(packageName,
PackageManager.GET_UNINSTALLED_PACKAGES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
2. 根据Intent判断,以下为判断代码:
public boolean checkApkExist(Context context, Intent intent) {
List
if(list.size() > 0){
return true;
}
return false;
}
public boolean checkApkExist(Context context, String packageName) {
if (packageName == null || “”.equals(packageName)) return false;
try {
ApplicationInfo info = context.getPackageManager() .getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES); return true;
} catch (NameNotFoundException e) { return false; }
}
public boolean checkApkExist(Context context, Intent intent) {
List
.queryIntentActivities(intent, 0);
if (list.size() > 0) {
return true;
}
return false;
}
全部回答
- 1楼网友:冷風如刀
- 2021-04-22 15:18
public static boolean isrunning(context context,string packagename){
activitymanager am = (activitymanager) context.getsystemservice(activity_service);
list infos = am.getrunningappprocesses();
for(runningappprocessinfo rapi : infos){
if(rapi.processname.equals(packagename))
return true;
}
return false;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯