2
2
3
3
import android .app .Activity ;
4
4
import android .content .Context ;
5
+ import android .content .Intent ;
5
6
import android .content .pm .PackageManager ;
7
+ import android .net .Uri ;
8
+ import android .os .Build ;
9
+ import android .provider .Settings ;
10
+ import android .support .annotation .Nullable ;
6
11
import android .support .v4 .app .ActivityCompat ;
7
12
13
+ import static android .os .Build .VERSION_CODES .FROYO ;
14
+ import static android .os .Build .VERSION_CODES .GINGERBREAD ;
8
15
import static android .support .v4 .content .PermissionChecker .checkSelfPermission ;
9
16
10
17
public final class PermissionUtils {
@@ -30,7 +37,7 @@ public static boolean verifyPermissions(int... grantResults) {
30
37
/**
31
38
* Returns true if the Activity or Fragment has access to all given permissions.
32
39
*
33
- * @param context context
40
+ * @param context context
34
41
* @param permissions permission list
35
42
* @return returns true if the Activity or Fragment has access to all given permissions.
36
43
*/
@@ -58,4 +65,33 @@ public static boolean shouldShowRequestPermissionRationale(Activity activity, St
58
65
}
59
66
return false ;
60
67
}
68
+
69
+ /**
70
+ * Creates an {@link Intent} that redirects the user to the app's settings page.
71
+ *
72
+ * @param context Context used to access the app's package name
73
+ * @return An intent that, when used to {@link Activity#startActivity(Intent)}, will open the running app's Settings page.
74
+ * If no matching Activity could be found for the device's settings, this method returns null.
75
+ */
76
+ public static @ Nullable Intent createAppSettingsIntent (Context context ) {
77
+ Intent intent ;
78
+ String packageName = context .getPackageName ();
79
+ if (Build .VERSION .SDK_INT >= GINGERBREAD ) {
80
+ // Utilize the dedicated Settings Action on API 9+
81
+ intent = new Intent (Settings .ACTION_APPLICATION_DETAILS_SETTINGS );
82
+ Uri uri = Uri .fromParts ("package" , packageName , null );
83
+ intent .setData (uri );
84
+
85
+ } else {
86
+ // Compatibility implementation using a View intent and the native Settings app
87
+ intent = new Intent (Intent .ACTION_VIEW );
88
+ intent .setFlags (Intent .FLAG_ACTIVITY_NEW_TASK );
89
+ intent .setClassName ("com.android.settings" , "com.android.settings.InstalledAppDetails" );
90
+ String extraKey = Build .VERSION .SDK_INT == FROYO ? "pkg" : "com.android.settings.ApplicationPkgName" ;
91
+ intent .putExtra (extraKey , packageName );
92
+ }
93
+
94
+ // If the intent can be used to resolve an Activity, return that intent. Otherwise, return null
95
+ return context .getPackageManager ().resolveActivity (intent , 0 ) != null ? intent : null ;
96
+ }
61
97
}
0 commit comments