下面列出了android.content.ActivityNotFoundException#printStackTrace ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* open system file manager
*
* @param activity activity
* @param type file type
* @param requestCode code
* @param extraMimeType mimiType must be arrays
*/
private static void select(Activity activity, String type, int requestCode,
String... extraMimeType) {
Intent intent = new Intent();
intent.setType(type);
intent.putExtra(Intent.EXTRA_MIME_TYPES, extraMimeType);
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
try {
activity.startActivityForResult(intent, requestCode);
} catch (ActivityNotFoundException e) {
intent.setAction(Intent.ACTION_GET_CONTENT);
try {
activity.startActivityForResult(intent, requestCode);
} catch (ActivityNotFoundException e2) {
e2.printStackTrace();
}
}
}
private void processIntentForOthers(@NonNull Intent intent) {
final ComponentName preferredCamera = Settings.getInstance().getPreferredCamera();
boolean launched = false;
if (preferredCamera != null) {
Log.d(TAG, "Launch preferred camera: " + preferredCamera.toString());
try {
onStartCameraApp(preferredCamera);
launched = true;
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Settings.getInstance().setPreferredCamera(null);
}
}
if (!launched) {
CameraChooserDialogFragment
.newInstance()
.show(getFragmentManager(), "CameraChooser");
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (code == requestCode) {
if (resultCode == Activity.RESULT_OK) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(getContext())) {
try {
Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
intent.setData(Uri.parse("package:" + getContext().getPackageName()));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
}
}
getActivity().finish();
}
}
public void generateInvoice(OrderEntity orderData) {
ActivityCompat.requestPermissions((BaseActivity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 666);
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED) {
if (Helper.getInstanse().generateInvoice(context, orderData) != null) {
File file = Helper.getInstanse().generateInvoice(context, orderData);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Uri path = Uri.fromFile(file);
Intent pdfOpenIntent = new Intent(Intent.ACTION_VIEW);
pdfOpenIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfOpenIntent.setDataAndType(path, "application/pdf");
try {
context.startActivity(pdfOpenIntent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
ToastHelper.showToast(context,
"No Application Available to View PDF",
Toast.LENGTH_SHORT);
}
} else
ToastHelper.showToast(context,
"ERROR in generating pdf",
Toast.LENGTH_SHORT);
}
}
public void printInvoice(OrderEntity orderData) {
ActivityCompat.requestPermissions((BaseActivity) context, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 666);
int result = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (result == PackageManager.PERMISSION_GRANTED) {
if (Helper.getInstanse().generateInvoice(context, orderData) != null) {
File file = Helper.getInstanse().generateInvoice(context, orderData);
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Uri path = Uri.fromFile(file);
Intent pdfOpenIntent = new Intent(Intent.ACTION_VIEW);
pdfOpenIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfOpenIntent.setDataAndType(path, "application/pdf");
try {
context.startActivity(pdfOpenIntent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
ToastHelper.showToast(context,
"No Application Available to View PDF",
Toast.LENGTH_SHORT);
}
} else
ToastHelper.showToast(context,
"ERROR in generating pdf",
Toast.LENGTH_SHORT);
}
}
private static void create(Activity activity, String type, int requestCode,
String... extraMimeType) {
Intent intent = new Intent();
// intent.setType(type);
//intent.putExtra(Intent.EXTRA_MIME_TYPES, extraMimeType);
intent.setAction(Intent.ACTION_OPEN_DOCUMENT_TREE);
try {
activity.startActivityForResult(intent, requestCode);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
private void viewOnGitHub() {
try {
startActivity(new Intent(Intent.ACTION_VIEW, GITHUB_URI));
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Toast.makeText(requireContext(), e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}
public static void launchUrl(String url, Context context) {
if (Utils.networkUnavailable(context)) {
Utils.toast(R.string.no_internet, context);
return;
}
try {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
context.startActivity(i);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
private void importFav() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/json");//设置类型
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(intent, 222);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
onMessage("抱歉,未找到文件管理器程序");
}
}
@Override
public void startActivity(Intent intent) {
try {
super.startActivity(intent);
} catch (ActivityNotFoundException e) {
Log.e(TAG, e.getMessage());
e.printStackTrace();
BaldToast.error(this);
}
}
private void displaySpeechRecognizer() {
try {
startActivityForResult(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
),
SPEECH_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
Log.e(TAG, S.str(e.getMessage()));
e.printStackTrace();
BaldToast.error(this);
}
}
public void displaySpeechRecognizer() {
try {
startActivityForResult(
new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
.putExtra(
RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM
),
SPEECH_REQUEST_CODE);
} catch (ActivityNotFoundException e) {
Log.e(TAG, S.str(e.getMessage()));
e.printStackTrace();
BaldToast.error(this);
}
}
private void importFav() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/json");//设置类型
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(intent, 222);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
onMessage("抱歉,未找到文件管理器程序");
}
}
public static boolean startIntentUrl(Activity activity, String intentFullUrl) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(intentFullUrl));
activity.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
e.printStackTrace();
return false;
}
}
/**
* 跳转到应用市场
*
* @param context 上下文
* @param packageName 应用包名
* @return 跳转是否成功
*/
public static boolean go2Market(Context context, String packageName) {
Uri uri = Uri.parse("market://details?id=" + packageName);
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
goToMarket.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
try {
context.startActivity(goToMarket);
return true;
} catch (ActivityNotFoundException e) {
e.printStackTrace();
return false;
}
}
private void goMeizuMainager() {
try {
Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra("packageName", packageName);
mContext.startActivity(intent);
} catch (ActivityNotFoundException localActivityNotFoundException) {
localActivityNotFoundException.printStackTrace();
goIntentSetting();
}
}
/**
* @param activity pass the current activity context
* @param path storage full path
*/
private static void fileChooser(Activity activity, String path) {
File file = new File(path);
Intent target = new Intent("android.intent.action.VIEW");
Uri uri = FileProvider.getUriForFile(activity, "com.package.name.fileprovider", file);
target.setDataAndType(uri, "application/pdf");
target.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent intent = Intent.createChooser(target, "Open File");
try {
activity.startActivity(intent);
} catch (ActivityNotFoundException var6) {
var6.printStackTrace();
}
}
/**
* Open a file not supported by Amaze
* @param f the file
* @param c
* @param forcechooser force the chooser to show up even when set default by user
*/
public static void openunknown(File f, Context c, boolean forcechooser) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
String type = MimeTypes.getMimeType(f);
Log.d(TAG, "openunknown " + f + ", " + forcechooser + ", " + type);
if (type != null && type.trim().length() != 0 && !type.equals("*/*")) {
//Uri uri=fileToContentUri(c, f);
//if(uri==null)
Uri uri=Uri.fromFile(f);
intent.setDataAndType(uri, type);
Intent startintent;
if (forcechooser) {
startintent = Intent.createChooser(intent, c.getResources().getString(R.string.openwith));
} else {
startintent = intent;
}
try {
c.startActivity(startintent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Toast.makeText(c, R.string.noappfound, Toast.LENGTH_SHORT).show();
openWith(f, c);
}
} else {
// failed to load mime type
openWith(f, c);
}
}
/**
* 用WPS 打开应用
*
* @param context Context 对象
* @param intent 设置好数据了类型的intent
* @return
*/
protected static boolean doRealWPSViewOrEdit(@NonNull ActivityStarterWrapper context, @NonNull Intent intent,
boolean readOnly, int requestCode) {
Bundle bundle = new Bundle();
bundle.putBoolean(WPSModel.SEND_CLOSE_BROAD, true); // 关闭时是否发送广播
bundle.putBoolean(WPSModel.SEND_SAVE_BROAD, true); // 保存时是否发送广播
bundle.putString(WPSModel.THIRD_PACKAGE, context.getPackageName()); // 第三方应用的包名,用于对改应用合法性的验证
if (readOnly) {
bundle.putString(WPSModel.OPEN_MODE, WPSModel.OpenMode.READ_ONLY); // 打开模式
} else {
bundle.putString(WPSModel.OPEN_MODE, WPSModel.OpenMode.NORMAL); // 打开模式
bundle.putBoolean(WPSModel.ENTER_REVISE_MODE, true); // 修订模式
}
bundle.putBoolean(WPSModel.CLEAR_TRACE, true);// 清除打开记录
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.setAction(Intent.ACTION_VIEW);
intent.setClassName(WPSModel.PackageName.NORMAL, WPSModel.ClassName.NORMAL);
intent.putExtras(bundle);
try {
if (readOnly) {
context.startActivity(Intent.createChooser(intent, "请选择WPS打开文件"));
} else {
context.startActivityForResult(Intent.createChooser(intent, "请选择WPS打开应用"), requestCode);
}
} catch (ActivityNotFoundException exception) {
Log.d("WPSOpenUtils", exception.getMessage());
exception.printStackTrace();
return false;
}
return true;
}
private boolean startApp(Uri uri) {
try {
Intent intent = new Intent();
intent.setData(uri);
intent.setAction(Intent.ACTION_VIEW);
mContext.startActivity(intent);
return true;
} catch (ActivityNotFoundException e) {
VenvyLog.i("打开支付包出错");
e.printStackTrace();
return false;
}
}