对于简单的intent显示跳转和隐式跳转就不写了,说说intent跳转的注意点
隐式intent跳转记得trycatch
对于intent隐式跳转时,如果不确定是否存在目标activity,需要对intent进行trycatch
editText.setOnClickListener(view -> {
Intent intent = new Intent();
intent.setAction("jjcc");
try {
startActivity(intent);
}catch (ActivityNotFoundException e){
Toast.makeText(this,"未找到指定的应用",Toast.LENGTH_SHORT).show();
}
});
强制显示选择器对话框
如果用户手机中存在很多个匹配的目标activity,会弹出选择器供用户选择,如果只是使用intent的话,用户可以选择始终选择一个,之后跳转便不会弹出弹窗
editText.setOnClickListener(view -> {
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
try {
startActivity(mapIntent);
}catch (ActivityNotFoundException e){
Toast.makeText(this,"未找到指定的应用",Toast.LENGTH_SHORT).show();
}
});
使用createChooser()来强制用户去选择
editText.setOnClickListener(view -> {
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
Intent chooser = Intent.createChooser(mapIntent, "");
try {
startActivity(chooser);
}catch (ActivityNotFoundException e){
Toast.makeText(this,"未找到指定的应用",Toast.LENGTH_SHORT).show();
}
});