Android Intent使用举例

版权声明:本文为博主项目中经验总结,著文备份,欢迎补充交流! https://blog.csdn.net/u013806583/article/details/72764322

1、筛选本地文件,通过uri获取文件的路径

/**
* 选择本地文件,获得文件的 uri 。
* 通过setType可以设置文件类型:比如筛选音视频文件、图片等等
*/
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(Intent.createChooser(intent, "choose file"), myRequestCode);

/**
* override 该方法,获得文件的 uri
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == myRequestCode) {
            //获得uri
            Uri uri = data.getData();

            //通过uri转化为文件的path:
            // https://github.com/shanxu100/GUtils
            String filePath = UriUtil.getFilePathFromUri(getApplicationContext(), uri);
     }
}

2、进入应用的详细设置页面


         /**
         * 参考博客
         * http://blog.csdn.net/cbbbc/article/details/60148864
         */

        Intent intent = new Intent();
        //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
        //判断SDK版本
        if(Build.VERSION.SDK_INT >= 9){

            intent.setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            //intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", getPackageName(), null));

        } else if(Build.VERSION.SDK_INT <= 8){

            intent.setAction(Intent.ACTION_VIEW);
            intent.setClassName("com.android.settings","com.android.settings.InstalledAppDetails");
            intent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
        }

        startActivityForResult(intent , myRequestCode);

3、 弹出“在其他应用上层显示”的设置页面,以及如何判断App拥有相关权限

             /**
             * 弹出在其他应用上显示的设置页面
             */
            if(Build.VERSION.SDK_INT >= 23) {
                Intent intent = new Intent();
                //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setAction(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                intent.setData(Uri.fromParts("package", getPackageName(), null));
                startActivity(intent);
            }

            /**
            * 如何判断App是否有该权限,可参考以下代码
            */


//target为具体的Activity的引用,即 ***Activity.this;
if (PermissionUtils.hasSelfPermissions(target, PERMISSION_NEEDSYSTEMALERTWINDOWPERMISSIN) || Settings.canDrawOverlays(target)) {
        //申请成功
        target.needSystemAlertWindowPermissin();
      } 
      else {

       if (!PermissionUtils.shouldShowRequestPermissionRationale(target, PERMISSION_NEEDSYSTEMALERTWINDOWPERMISSIN)) {
       //不再提示
          target.OnNeverAskAgain();

        } else {
        //申请权限拒绝
          target.OnPermissionDenied();
        }
      }

4、在Application中启动Activity

//在App.java 文件中,App类 继承 Application,并在Manifest中修改了<application></application>标签中的 name 属性

Intent intent = new Intent(appContext, ***Activity.class);

//必须:开启新的任务队列
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

5、指定URL,用浏览器打开

    String url = "https://github.com/liaohuqiu/android-UCToast";
    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(myIntent);

猜你喜欢

转载自blog.csdn.net/u013806583/article/details/72764322