![](/img/trans.png)
[英]CheckBox Preference Activity how to start and stop service from Intent.ACTION_BOOT_COMPLETED using broadcast receiver
[英]How to start an activity from intent using broadcast?
我正在使用代碼在Android中調用意圖
Intent intent = new Intent();
String PACKAGE_NAME="com...."
intent.setPackage(PACKAGE_NAME);
intent.setAction(Intent.ACTION_VOICE_COMMAND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
getApplication().startActivity(intent);
不幸的是,在某些情況下,我不知道PACKAGE_NAME。 因此,另一種方法是使用廣播。 如何使用? 謝謝大家
如果您不知道該活動的軟件包和名稱 ,則無法直接致電該活動。
由於您不知道程序包和活動名稱,因此只能要求 OS顯示您的Intent Action的所有可能變體。 在大多數情況下,用戶單擊“始終為此操作打開此應用程序”,這意味着您將直接打開另一個應用程序。
因此,在您的情況下,您的代碼應類似於
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VOICE_COMMAND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
//dont forget to check if user has at least one application for your Intent Action
if (intent.resolveActivity(getPackageManager()) != null) {
context.startActivity(intent);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.