sample: 自定义控件; fix: ActivityNotFoundException

This commit is contained in:
hyb1996
2018-10-14 10:33:23 +08:00
parent db3571851c
commit 14b7a7e85e
39 changed files with 615 additions and 220 deletions

View File

@@ -64,7 +64,7 @@ public class PFile extends File {
if (renameTo(newFile)) {
return newFile;
} else {
return null;
return this;
}
}

View File

@@ -6,6 +6,9 @@ import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.widget.Toast;
import com.stardust.R;
import java.io.File;
@@ -35,20 +38,26 @@ public class IntentUtil {
}
public static void sendMailTo(Context context, String sendTo, @Nullable String title, @Nullable String content) {
Uri uri = Uri.parse("mailto:" + sendTo);
String[] email = {sendTo};
Intent intent = new Intent(Intent.ACTION_SENDTO, uri).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_CC, email);
if (title != null)
intent.putExtra(Intent.EXTRA_SUBJECT, title);
if (content != null)
intent.putExtra(Intent.EXTRA_TEXT, content);
context.startActivity(Intent.createChooser(intent, ""));
public static boolean sendMailTo(Context context, String sendTo, @Nullable String title, @Nullable String content) {
try {
Uri uri = Uri.parse("mailto:" + sendTo);
String[] email = {sendTo};
Intent intent = new Intent(Intent.ACTION_SENDTO, uri).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_CC, email);
if (title != null)
intent.putExtra(Intent.EXTRA_SUBJECT, title);
if (content != null)
intent.putExtra(Intent.EXTRA_TEXT, content);
context.startActivity(Intent.createChooser(intent, ""));
return true;
} catch (ActivityNotFoundException e) {
e.printStackTrace();
return false;
}
}
public static void sendMailTo(Context context, String sendTo) {
sendMailTo(context, sendTo, null, null);
public static boolean sendMailTo(Context context, String sendTo) {
return sendMailTo(context, sendTo, null, null);
}
public static boolean browse(Context context, String link) {
@@ -62,10 +71,16 @@ public class IntentUtil {
}
public static void shareText(Context context, String text) {
context.startActivity(new Intent(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_TEXT, text)
.setType("text/plain"));
public static boolean shareText(Context context, String text) {
try {
context.startActivity(new Intent(Intent.ACTION_SEND)
.putExtra(Intent.EXTRA_TEXT, text)
.setType("text/plain"));
return true;
} catch (ActivityNotFoundException e) {
e.printStackTrace();
return false;
}
}
public static boolean goToAppDetailSettings(Context context, String packageName) {
@@ -85,7 +100,7 @@ public class IntentUtil {
return goToAppDetailSettings(context, context.getPackageName());
}
public static void installApk(Context context, String path, String fileProviderAuthority) {
public static void installApk(Context context, String path, String fileProviderAuthority) throws ActivityNotFoundException {
Uri uri = getUriOfFile(context, path, fileProviderAuthority);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
@@ -95,9 +110,18 @@ public class IntentUtil {
context.startActivity(intent);
}
public static void viewFile(Context context, String path, String fileProviderAuthority) {
public static void installApkOrToast(Context context, String path, String fileProviderAuthority) {
try {
installApk(context, path, fileProviderAuthority);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
Toast.makeText(context, R.string.error_activity_not_found_for_apk_installing, Toast.LENGTH_SHORT).show();
}
}
public static boolean viewFile(Context context, String path, String fileProviderAuthority) {
String mimeType = MimeTypes.fromFileOr(path, "*/*");
viewFile(context, path, mimeType, fileProviderAuthority);
return viewFile(context, path, mimeType, fileProviderAuthority);
}
public static Uri getUriOfFile(Context context, String path, String fileProviderAuthority) {
@@ -110,22 +134,34 @@ public class IntentUtil {
return uri;
}
public static void viewFile(Context context, String path, String mimeType, String fileProviderAuthority) {
Uri uri = getUriOfFile(context, path, fileProviderAuthority);
context.startActivity(new Intent(Intent.ACTION_VIEW)
.setDataAndType(uri, mimeType)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION));
public static boolean viewFile(Context context, String path, String mimeType, String fileProviderAuthority) {
try {
Uri uri = getUriOfFile(context, path, fileProviderAuthority);
context.startActivity(new Intent(Intent.ACTION_VIEW)
.setDataAndType(uri, mimeType)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION));
return true;
} catch (ActivityNotFoundException e) {
e.printStackTrace();
return false;
}
}
public static void editFile(Context context, String path, String fileProviderAuthority) {
String mimeType = MimeTypes.fromFileOr(path, "*/*");
Uri uri = getUriOfFile(context, path, fileProviderAuthority);
context.startActivity(new Intent(Intent.ACTION_EDIT)
.setDataAndType(uri, mimeType)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION));
public static boolean editFile(Context context, String path, String fileProviderAuthority) {
try {
String mimeType = MimeTypes.fromFileOr(path, "*/*");
Uri uri = getUriOfFile(context, path, fileProviderAuthority);
context.startActivity(new Intent(Intent.ACTION_EDIT)
.setDataAndType(uri, mimeType)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION));
return true;
} catch (ActivityNotFoundException e) {
e.printStackTrace();
return false;
}
}
}

View File

@@ -1,4 +1,5 @@
<resources>
<string name="app_name">common</string>
<string name="text_no_floating_window_permission">没有悬浮窗权限</string>
<string name="error_activity_not_found_for_apk_installing">找不到安装apk的应用</string>
</resources>