<分区>
Possible Duplicate: How can I check if an app running on Android?
我想知道是否有办法检查Android默认浏览器(Android OS中的浏览器)是否已打开。假设它打开了,我想显示一个 toast 。我如何以编程方式执行此操作?
这是我在扩展服务类的类中使用的代码,该类监听日志并检查是否已打开 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());
}