Activity 常见的意图整理

整理一些常用的关于Activity

image.png

电话

Intent.ACTION_DIAL 跳转到拨打电话的页面

 Uri data = Uri.parse("tel:" + "15523872***");
    intent.setData(data);//指定电话号码

Intent.ACTION_CALL 直接打电话 需要申请权限(android 6.0 后动态申请权限 我用的easyPermission)

联系人

Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Contacts.People.CONTENT_URI);
        startActivity(intent);

短信

        Uri uri = Uri.parse("smsto:10010");
        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
        intent.putExtra("sms_body", "Hello");
        startActivity(intent);

游览器

 Uri uri = Uri.parse("http://www.baidu.com");
        Intent intent  = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent); 

邮件

Intent email = new Intent(android.content.Intent.ACTION_SEND);
        email.setType("plain/text");
        String  emailTitle = "标题";
        String emailContent = "内容";
        String[] emailReciver = new String[]{"[email protected]"};
        email.putExtra(android.content.Intent.EXTRA_EMAIL, emailReciver);
        email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailTitle);
        email.putExtra(android.content.Intent.EXTRA_TEXT, emailContent);
        startActivity(email);

多媒体 (视屏)

```
    Intent intent = new Intent(Intent.ACTION_VIEW);
    String path = Environment.getExternalStorageDirectory().getPath()+ "/1.mp4";//该路径可以自定义
    File file = new File(path);
    Uri uri = Uri.fromFile(file);
    intent.setDataAndType(uri, "video/*");
    startActivity(intent);
```

摄像头

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
     startActivityForResult(intent, 0);

系统图库

 Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, VALUE_PICK_PICTURE);

裁剪图片

 Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(bitmap, "image/*");
        // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        intent.putExtra("crop", "true");
        intent.putExtra("scale", true);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // 裁剪后输出图片的尺寸大小
        intent.putExtra("outputX", 250);
        intent.putExtra("outputY", 250);
        intent.putExtra("return-data", true);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        startActivityForResult(intent, 0);

网路设置

  Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
        startActivityForResult(intent, 0);

源码

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.Contacts;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

public class EveryOpenActivity extends AppCompatActivity implements View.OnClickListener, EasyPermissions.PermissionCallbacks {

    Button bt_0;
    Button bt_1;
    Button bt_2;
    Button bt_3;
    Button bt_4;
    Button bt_5;
    Button bt_6;
    Button bt_7;
    Button bt_8;
    Button bt_9;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_every_open);
        bt_0 = findViewById(R.id.bt_0);
        bt_1 = findViewById(R.id.bt_1);
        bt_2 = findViewById(R.id.bt_2);
        bt_3 = findViewById(R.id.bt_3);
        bt_4 = findViewById(R.id.bt_4);
        bt_5 = findViewById(R.id.bt_5);
        bt_6 = findViewById(R.id.bt_6);
        bt_7 = findViewById(R.id.bt_7);
        bt_8 = findViewById(R.id.bt_8);
        bt_9 = findViewById(R.id.bt_9);

        bt_0.setOnClickListener(this);
        bt_1.setOnClickListener(this);
        bt_2.setOnClickListener(this);
        bt_3.setOnClickListener(this);
        bt_4.setOnClickListener(this);
        bt_5.setOnClickListener(this);
        bt_6.setOnClickListener(this);
        bt_7.setOnClickListener(this);
        bt_8.setOnClickListener(this);
        bt_9.setOnClickListener(this);


    }

    private void start0() {

//        Intent intent = new Intent(Intent.ACTION_DIAL, uri);
//        startActivity(intent);
        if (EasyPermissions.hasPermissions(this,Manifest.permission.CALL_PHONE)) {
            Uri uri = Uri.parse("tel:10010");
            Intent intent = new Intent(Intent.ACTION_CALL, uri);
            startActivity(intent);
        }else {
            EasyPermissions.requestPermissions(this, "打电话",1,Manifest.permission.CALL_PHONE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this);
    }

    @Override

    public void onPermissionsGranted(int requestCode, @NonNull List perms) {
       if (perms.contains(Manifest.permission.CALL_PHONE)){
           Uri uri = Uri.parse("tel:10010");
           Intent intent = new Intent(Intent.ACTION_CALL, uri);
           startActivity(intent);
       }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List perms) {
        if (!EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this)
                    .setRationale("此功能需要" + "电话权限" + "权限,否则无法正常使用,是否打开设置")
                    .setPositiveButton("是")
                    .setNegativeButton("否").build().show();
        }
    }

    private void start1(){
        Uri uri = Uri.parse("smsto:10010");
        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
        intent.putExtra("sms_body", "Hello");
        startActivity(intent);
    }

    private void start2(){
        Uri uri = Uri.parse("http://www.baidu.com");
        Intent intent  = new Intent(Intent.ACTION_VIEW, uri);
        startActivity(intent);
    }

    private void start3(){

        Intent email = new Intent(android.content.Intent.ACTION_SEND);
        email.setType("plain/text");
        String  emailTitle = "标题";
        String emailContent = "内容";
        String[] emailReciver = new String[]{"[email protected]"};
        email.putExtra(android.content.Intent.EXTRA_EMAIL, emailReciver);
        email.putExtra(android.content.Intent.EXTRA_SUBJECT, emailTitle);
        email.putExtra(android.content.Intent.EXTRA_TEXT, emailContent);
        startActivity(email);

    }

    private void start4(){

        Intent intent = new Intent(Intent.ACTION_VIEW);
        String path = Environment.getExternalStorageDirectory().getPath()+ "/1.mp4";//该路径可以自定义
        File file = new File(path);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "video/*");
        startActivity(intent);

    }

    private void start5(){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 0);

    }


    public static final int CROP_REQUEST = 2;
    Uri bitmap;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case VALUE_PICK_PICTURE: // 如果是直接从相册获取
                    bitmap = data.getData();//拿到所选图片的Uri
                    break;
            }
        }


    }

    private void start6(){
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(bitmap, "image/*");
        // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        intent.putExtra("crop", "true");
        intent.putExtra("scale", true);
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // 裁剪后输出图片的尺寸大小
        intent.putExtra("outputX", 250);
        intent.putExtra("outputY", 250);
        intent.putExtra("return-data", true);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", true); // no face detection
        startActivityForResult(intent, 0);

    }

    private void start7(){
        Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
        startActivityForResult(intent, 0);
    }

    private void start8(){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        intent.setData(Contacts.People.CONTENT_URI);
        startActivity(intent);
    }
    public static final int VALUE_PICK_PICTURE = 1;
    private void start9() {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent, VALUE_PICK_PICTURE);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_0:
                start0();
                break;
            case R.id.bt_1:
                start1();
                break;
            case R.id.bt_2:
                start2();
                break;
            case R.id.bt_3:
                start3();
                break;
            case R.id.bt_4:
                start4();
                break;
            case R.id.bt_5:
                start5();
                break;
            case R.id.bt_6:
                start6();
                break;
            case R.id.bt_7:
                start7();
                break;
            case R.id.bt_8:
                start8();
                break;
            case R.id.bt_9:
                start9();
                break;
        }

    }
}

你可能感兴趣的:(Activity 常见的意图整理)