可能重复: 如何检查应用程序是否在 Android 上运行?
我想知道是否有办法检查 Android 默认浏览器(Android OS 中的浏览器)是否已打开。假设它已打开,我想展示一个祝酒词。如何以编程方式执行此操作?
这是我在扩展 Service 类的类中使用的代码,该类监听日志并检查 Android 默认浏览器是否已打开。如果是,那么它会在LogCat中打印一条日志,说明 Android 默认浏览器已打开。
try {
Process mLogcatProc = null;
BufferedReader reader = null;
mLogcatProc = Runtime.getRuntime().exec(new String[] { "logcat", "-d" });
reader = new BufferedReader(new InputStreamReader(mLogcatProc.getInputStream()));
String line;
final StringBuilder log = new StringBuilder();
String separator = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
log.append(line);
log.append(separator);
}
String w = log.toString();
Log.d("LogService", "The log is: " + w);
if (w.contains("Starting activity: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.android.browser/.BrowserActivity }")) {
Log.d("LogService", "The browser has been opened");
}
}
catch (Exception e) {
Log.d("LogService", "The stacktrace is: " + e.getMessage());
}