我正在从活动中启动设备的标准计算器,但如果我关闭计算器,则不会返回启动计算器的活动。我是否必须设置某个标记?
代码:
public void LaunchDeviceCalculator(){
ArrayList<HashMap<String,Object>> packages =new ArrayList<HashMap<String,Object>>();
final PackageManager pm = getPackageManager();
List<PackageInfo> packs = pm.getInstalledPackages(0);
for (PackageInfo pi : packs) {
if( pi.packageName.toString().toLowerCase().contains("calcul")){
HashMap<String, Object> map = new HashMap<String, Object>();
map.put("appName", pi.applicationInfo.loadLabel(pm));
map.put("packageName", pi.packageName);
packages.add(map);
}
}
if(packages.size()>=1){
String packageName = (String) packages.get(0).get("packageName");
Intent i = pm.getLaunchIntentForPackage(packageName);
if (i != null)
startActivity(i);
}
}
答案 0 :(得分:1)
对于它的价值,第三方计算器处理这种情况: http://www.efalk.org/RpnCalcFinancial/
搜索开发人员备注。
开发人员列出将启动应用程序的Intent以及返回的Intent和extras。
据我记忆,它是免费的。
它确实使用反向波兰表示法,即RPN。
答案 1 :(得分:0)
启动计算器可以通过以下代码完成。请查看此链接以获取更多详细信息:Android Intents For App Integration (Call a Calculator, Play Video, Open an Editor)
我还没试过这个但是代码看起来很简单。如果有效,请告诉我。
// activity name and package for stock calculator
private static final String CALCULATOR_PACKAGE_NAME = "com.android.calculator2";
private static final String CALCULATOR_CLASS_NAME = "com.android.calculator2.Calculator";
public void launchCalculator() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new Component Name(CALCULATOR_PACKAGE_NAME, CALCULATOR_CLASS_NAME));
try {
this.start Activity(intent);
} catch (ActivityNotFoundException noSuchActivity) {
// handle exception where calculator intent filter is not registered
}
}