下面列出了android.content.Context#startActivity ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void startInstall(Context context, File apkfile) {
if (!apkfile.exists()) {
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(context, context.getString(R.string.updatefun_provider_file_authorities), apkfile);
intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
} else {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file://" + apkfile.toString()),
"application/vnd.android.package-archive");
}
context.startActivity(intent);
}
@Override public void onReceive(Context context, Intent intent) {
String url = intent.getDataString();
if (url != null) {
Set<String> listOfPackagesThatResolveUri = getNativeAppPackage(context, Uri.parse(url));
String availableNativeAppPackageName = null;
if (listOfPackagesThatResolveUri.iterator()
.hasNext()) {
availableNativeAppPackageName = listOfPackagesThatResolveUri.iterator()
.next();
}
if (availableNativeAppPackageName != null) {
Intent launchIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
Bundle httpHeaders = new Bundle();
httpHeaders.putString(REFERER_ATTRIBUTE, REFERER_VALUE);
launchIntent.putExtra(Browser.EXTRA_HEADERS, httpHeaders);
launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(launchIntent);
} else {
Toast.makeText(context, "No application to open.", Toast.LENGTH_SHORT)
.show();
}
}
}
public static void launchMain (Context context) {
// Intent intent = new Intent(Intent.ACTION_MAIN);
// intent.addCategory(Intent.CATEGORY_LAUNCHER);
// context.startActivity(intent);
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
ComponentName cn = new ComponentName("com.example.a90678.wechat_group_send_17_07_02_17_35",
"com.example.a90678.wechat_group_send_17_07_02_17_35.access.MainActivity");
intent.setComponent(cn);
if (intent == null) {
L.e("intent is null ");
return;
}
;
context.startActivity(intent);
}
public static void shareURL(Context context,String title, String url){
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT,title+": "+url);
sendIntent.setType("text/plain");
Intent clipboardIntent = new Intent(context, CopyToClipboardActivity.class);
clipboardIntent.setData(Uri.parse(url));
Intent chooserIntent = Intent.createChooser(sendIntent,context.getString(R.string.share_with));
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { clipboardIntent });
context.startActivity(chooserIntent);
}
/**
* 带有 flag 和序列化参数的 finish 跳转
*
* @param context 上下文对象
* @param target 目标
* @param flags 条件
*/
protected static void forward(Context context, Class<? extends Activity> target, int flags, Parcelable parcelable) {
Intent intent = new Intent(context, target);
setFlags(intent, flags);
putParams(intent, parcelable);
context.startActivity(intent);
if (isActivity(context)) {
((Activity) context).finish();
}
}
public static void openFile(final Context context, final File target) {
final String mime = MimeTypes.getMimeType(target);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider", target);
intent.setDataAndType(contentUri, mime);
if (mime != null) {
intent.setDataAndType(contentUri, mime);
} else {
intent.setDataAndType(contentUri, "*/*");
}
} else {
if (mime != null) {
intent.setDataAndType(Uri.fromFile(target), mime);
} else {
intent.setDataAndType(Uri.fromFile(target), "*/*");
}
}
if (context.getPackageManager().queryIntentActivities(intent, 0).isEmpty()) {
Toast.makeText(context, R.string.cantopenfile, Toast.LENGTH_SHORT).show();
return;
}
try {
context.startActivity(intent);
} catch (Exception e) {
Toast.makeText(context, context.getString(R.string.cantopenfile) + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
public void onServiceError(int status,String info){
if(status == API.CODE.LOGIN_INVALID){
Context ctx = JActivityManager.getInstance().currentActivity();
ctx.startActivity(new Intent(ctx, LoginActivity.class));
return;
}
JUtils.Toast(info);
}
public static void launch(Context activity, int from, String title, String icon, int id, int type, int cid, String year, String director, String actor, long timeLength, int order, int vid, int mode, String comment, String staticsId, String fragId) {
LogInfo.log("fornia", "标题 title:" + title + "评论或者角色 mode:" + mode + "comment:" + comment);
mActivity = null;
Intent intent = new Intent(activity, SharePageEditActivity.class);
intent.putExtra("from", from);
if (mode == -1) {
intent.putExtra("launchMode", 2);
intent.putExtra("comment", comment);
} else if (mode == 5) {
intent.putExtra("launchMode", 12);
intent.putExtra("comment", comment);
} else if (mode == 4) {
intent.putExtra("launchMode", 11);
intent.putExtra("comment", comment);
} else if (mode == 6) {
intent.putExtra("launchMode", 13);
intent.putExtra("role", comment);
}
intent.putExtra("title", title);
intent.putExtra(SettingsJsonConstants.APP_ICON_KEY, icon);
intent.putExtra("id", id);
intent.putExtra("type", type);
intent.putExtra("cid", cid);
intent.putExtra("year", year);
intent.putExtra("director", director);
intent.putExtra("actor", actor);
intent.putExtra("timeLength", timeLength);
intent.putExtra("order", order);
intent.putExtra("vid", vid);
intent.putExtra(PlayConstant.LIVE_MODE, mode);
intent.putExtra("staticsId", staticsId);
intent.putExtra("fragId", fragId);
activity.startActivity(intent);
}
/**
* start activity from {@link Context}
*
* @param context context we start from
* @param intent intent to start
* @param options options used to start activity
* @return {@code true} if we start it safely, {@code false} if it's unsafe so we didn't start
* it
*/
public static boolean startActivitySafely(@NonNull Context context, @NonNull Intent intent,
Bundle options) {
if (isIntentSafe(context.getPackageManager(), intent)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
context.startActivity(intent, options);
} else {
context.startActivity(intent);
}
return true;
}
return false;
}
/**
* Android 4.4 系统上文件查看
*
* @param context
* @param fileName
*/
public static void viewFile4_4(Context context, String fileName) {
String ext = getExtension(fileName);
if (TextUtils.isEmpty(ext)) {
AndroidUtils.showToast(context, context.getString(R.string.file_not_recognized));
return;
}
Intent intent = IntentUtils.getFileViewIntent(context, fileName);
if (intent != null) {
Intent newIntent = Intent.createChooser(intent, context.getString(R.string.choose_one_activity_to_open));
context.startActivity(newIntent);
} else {
FileViewerUtils.viewFile(context, fileName);
}
}
/**
* 打开多媒体文件.
*
* @param context 上下文
* @param file 多媒体文件
*/
public static void playSound(Context context, File file) {
try {
// 调用系统程序打开文件.
Intent intent = new Intent(Intent.ACTION_VIEW);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// intent.setClassName("com.android.music", "com.android.music.MediaPlaybackActivity");
intent.setDataAndType(Uri.fromFile(file), "audio/*");
context.startActivity(intent);
} catch (Exception ex) {
Toast.makeText(context, "打开失败.", Toast.LENGTH_SHORT).show();
}
}
public static void requestEnableBluetooth(final Context context, final ResultReceiver resultReceiver) {
Intent callIntent = new Intent(context, BleEnableActivity.class);
callIntent.putExtra(EXTRA_CALLBACK, resultReceiver);
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(callIntent);
}
public void open(Context context, boolean isClearStack) {
Intent intent = new Intent(context, WalletsActivity.class);
if (isClearStack) {
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
}
context.startActivity(intent);
}
/**
* 打开浏览器
*/
public static void openBrowser(Context context, String address){
if (TextUtils.isEmpty(address) || address.startsWith("file://")) {
CommonUtils.toastShow(context.getString(R.string.article_browser_error));
return;
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(address));
if(context.getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY) != null){
context.startActivity(intent);
}else {
CommonUtils.toastShow(context.getString(R.string.open_browser_unknown));
}
}
/**
* Launches navigation in Google Maps.
*
* @throws ActivityNotFoundException if no navigation application
*/
public static void launchNavigation(Context context, double latitude, double longitude) {
Intent intent = createLaunchNavigationIntent(latitude, longitude);
checkContext(context, intent);
context.startActivity(intent);
}
public static void startActivity(Context context) {
Intent intent = new Intent(context, CollectionActivity.class);
context.startActivity(intent);
}
/**
* 根据文件类型打开文件
*/
public static void openFileByType(Context context, String type, File file) {
Intent intent = null;
if (StringUtil.isNotBlank(type) && file.isFile()) {
if (checkEndsWithInFileType(type, getStringArray(context, R.array.file_type_text))) {
intent = OpenFileIntent.getTextFileIntent(file);
} else if (checkEndsWithInFileType(type, getStringArray(context, R.array.file_type_image))) {
intent = OpenFileIntent.getImageFileIntent(file);
} else if (checkEndsWithInFileType(type, getStringArray(context, R.array.file_type_pdf))) {
intent = OpenFileIntent.getPdfFileIntent(file);
} else if (checkEndsWithInFileType(type, getStringArray(context, R.array.file_type_html))) {
intent = OpenFileIntent.getHtmlFileIntent(file);
} else if (checkEndsWithInFileType(type, getStringArray(context, R.array.file_type_word))) {
intent = OpenFileIntent.getWordFileIntent(file);
} else if (checkEndsWithInFileType(type, getStringArray(context, R.array.file_type_excel))) {
intent = OpenFileIntent.getExcelFileIntent(file);
} else if (checkEndsWithInFileType(type, getStringArray(context, R.array.file_type_ppt))) {
intent = OpenFileIntent.getPPTFileIntent(file);
} else if (checkEndsWithInFileType(type, getStringArray(context, R.array.file_type_apk))) {
intent = OpenFileIntent.getApkFileIntent(file);
} else if (checkEndsWithInFileType(type, getStringArray(context, R.array.file_type_audio))) {
intent = OpenFileIntent.getAudioFileIntent(file);
} else if (checkEndsWithInFileType(type, getStringArray(context, R.array.file_type_video))) {
intent = OpenFileIntent.getVideoFileIntent(file);
} else if ((checkEndsWithInFileType(type, getStringArray(context, R.array.file_type_rar))) ||
checkEndsWithInFileType(type, getStringArray(context, R.array.file_type_zip))) {
intent = OpenFileIntent.getZipFileIntent(file);
}
if (file.exists() && intent != null) {
try {
context.startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
MainApp.showToastMsg("can't open this file, please install related app to open it.");
}
} else {
MainApp.showToastMsg("can't open this file.");
}
} else {
MainApp.showToastMsg("can't open this file.");
}
}
public static void launch(Context context) {
context.startActivity(new Intent(context, UseRxJavaRightWayActivity.class));
}
private void enterSms(Context context, String phone) {
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + phone));
context.startActivity(intent);
}
public static void gotoFragmentAbout(Context context) {
Intent intent = new Intent(context, ActivityUserMore.class);
intent.putExtra(ARG_FRAGMENT_TAG,
FragmentAbout.class.getSimpleName());
context.startActivity(intent);
}