美文网首页
通用Intent

通用Intent

作者: zbbwzz | 来源:发表于2018-06-14 17:07 被阅读0次

    Intent 用于通过描述您想在某个 `[Intent] 对象中执行的简单操作(如“查看地图”或“拍摄照片”)来启动另一应用中的某个 Activity。 这种 Intent 称作隐式 Intent,因为它并不指定要启动的应用组件,而是指定一项操作并提供执行该操作所需的一些数据。

    当您调用 [startActivity()][startActivityForResult()] 并向其传递隐式 Intent 时,系统会 [将 Intent 解析]为可处理该 Intent 的应用并启动其对应的 [Activity]。 如果有多个应用可处理 Intent,系统会为用户显示一个对话框,供其选择要使用的应用。

    MainActivity程序起点

    private Button alarmButton;
    private Button eventButton;
    private Button caButton;
    private Button cactButton;
    private Button cactInsertButton;
    private Button emailButton;
    private Button emailAppButton;
    private Button catButton;
    private Button mapButton;
    private Button audioButton;
    private Button phoneButton;
    private Button bowerButton;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        alarmButton = findViewById(R.id.button_alarm);
        eventButton = findViewById(R.id.button_event);
        caButton = findViewById(R.id.button_ca);
        cactButton = findViewById(R.id.button_cact);
        cactInsertButton = findViewById(R.id.button_cact_inset);
        emailButton = findViewById(R.id.button_email);
        emailAppButton = findViewById(R.id.button_email_app);
        catButton = findViewById(R.id.button_cat);
        mapButton = findViewById(R.id.button_map);
        audioButton = findViewById(R.id.button_audio);
        phoneButton = findViewById(R.id.button_phone);
        bowerButton = findViewById(R.id.button_bower);
    
        alarmButton.setOnClickListener(this);//设置系统闹钟
        eventButton.setOnClickListener(this);//添加日历
        caButton.setOnClickListener(this);//打开系统相机
        cactButton.setOnClickListener(this);//打开特定联系人
        cactInsertButton.setOnClickListener(this);//插入联系人
        emailButton.setOnClickListener(this);//打开电子邮件
        emailAppButton.setOnClickListener(this);//打开电子邮件
        catButton.setOnClickListener(this);//叫车
        mapButton.setOnClickListener(this);//地图应用
        audioButton.setOnClickListener(this);//播放音乐
        phoneButton.setOnClickListener(this);//打电话
        bowerButton.setOnClickListener(this);//打开浏览器
    }
    
    /**
     * 打开浏览器
     *
     * @param url
     */
    public void openWebPage(String url) {
        Uri webpage = Uri.parse(url);
        Intent intent = new Intent(Intent.ACTION_VIEW, webpage);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    /**
     * 打电话
     */
    public void callMe(String phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + phoneNumber));
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    /**
     * 播放特定歌手音乐
     */
    public void playSearchArtist(String artist) {
        Toast.makeText(this, "播放音乐", Toast.LENGTH_SHORT);
        Intent intent = new Intent(MediaStore.INTENT_ACTION_MEDIA_PLAY_FROM_SEARCH);
        intent.putExtra(MediaStore.EXTRA_MEDIA_FOCUS,
                MediaStore.Audio.Artists.ENTRY_CONTENT_TYPE);
        intent.putExtra(MediaStore.EXTRA_MEDIA_ARTIST, artist);
        intent.putExtra(SearchManager.QUERY, artist);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    /**
     * 打开地图应用
     *
     * @param geoLocation
     */
    public void showMap(Uri geoLocation) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(geoLocation);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    /**
     * 叫车用用
     */
    public void callCar() {
        //Intent intent = new Intent(ReserveIntents.ACTION_RESERVE_TAXI_RESERVATION);
        //if (intent.resolveActivity(getPackageManager()) != null) {
        //     startActivity(intent);
        // }
    }
    
    /**
     * 打开电子邮件,app
     */
    public void composeEmail(String[] addresses, String subject) {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        intent.setData(Uri.parse("mailto:")); // only email apps should handle this
        intent.putExtra(Intent.EXTRA_EMAIL, addresses);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    /**
     * 打开电子邮件,本地手机
     */
    public void composeEmail(String[] addresses, String subject, Uri attachment) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("*/*");
        intent.putExtra(Intent.EXTRA_EMAIL, addresses);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_STREAM, attachment);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    /**
     * 插入联系人
     */
    public void insertContact(String name, String email) {
        Intent intent = new Intent(Intent.ACTION_INSERT);
        intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
        intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
        intent.putExtra(ContactsContract.Intents.Insert.EMAIL, email);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    /**
     * 选择特定联系人
     */
    static final int REQUEST_SELECT_PHONE_NUMBER = 1;
    
    public void selectContact() {
        // Start an activity for the user to pick a phone number from contacts
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, REQUEST_SELECT_PHONE_NUMBER);
        }
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        //联系人回调
        if (requestCode == REQUEST_SELECT_PHONE_NUMBER && resultCode == RESULT_OK) {
            // Get the URI and query the content provider for the phone number
            Uri contactUri = data.getData();
            String[] projection = new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER};
            Cursor cursor = getContentResolver().query(contactUri, projection,
                    null, null, null);
            // If the cursor returned is valid, get the phone number
            if (cursor != null && cursor.moveToFirst()) {
                int numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                String number = cursor.getString(numberIndex);
                // Do something with the phone number
            }
        }
        //  相机回调
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bitmap thumbnail = data.getParcelableExtra("data");
            // Do other work with full size photo saved in mLocationForPhotos
        }
    }
    
    /**
     * 启动相机
     */
    static final int REQUEST_IMAGE_CAPTURE = 1;
    
    //static final Uri mLocationForPhotos;
    public void captureOhpto(String targetFileNmae) {
        //Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//相机模式启动
        Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);//视频模式启动
        //intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.withAppendedPath(mLocationForPhotos,targetFileNmae));
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    /**
     * 添加日历
     */
    public void addEvent(String title, String location, Calendar begin, Calendar end) {
        Intent intent = new Intent(Intent.ACTION_INSERT)
                .setData(CalendarContract.Events.CONTENT_URI)
                .putExtra(CalendarContract.Events.TITLE, title)
                .putExtra(CalendarContract.Events.EVENT_LOCATION, location)
                .putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, begin)
                .putExtra(CalendarContract.EXTRA_EVENT_END_TIME, end);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    /**
     * 创建闹钟
     */
    public void createAlarm(String message, int hour, int minutes) {
    
        Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
                .putExtra(AlarmClock.EXTRA_MESSAGE, message)
                .putExtra(AlarmClock.EXTRA_HOUR, hour)
                .putExtra(AlarmClock.EXTRA_MINUTES, minutes);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
    
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_alarm:
                createAlarm("打开创建系统闹钟页面", 14, 27);
                break;
            case R.id.button_event:
                addEvent("添加日历", "我也不知道该写什么", Calendar.getInstance(), Calendar.getInstance());
                break;
            case R.id.button_ca:
                captureOhpto("抛射图片");
                break;
            case R.id.button_cact:
                selectContact();
                break;
            case R.id.button_cact_inset:
                insertContact("周大王", "1102344710@qq.com");
                break;
            case R.id.button_email:
                composeEmail(new String[]{"1102344710", "1112344710"}, "这是主题", Uri.EMPTY);
                break;
            case R.id.button_email_app:
                composeEmail(new String[]{"1102344710", "1112344710"}, "这是主题");
                break;
            case R.id.button_cat:
                Toast.makeText(this, "不知道为啥不能使用", Toast.LENGTH_SHORT);
                break;
            case R.id.button_map:
                String url = "http://api.map.baidu.com/direction?origin=33.988177,118.786991&destination=32.047616,118.790609&mode=driving&output=";
                Uri uri = Uri.parse(url);
                showMap(uri);
                break;
            case R.id.button_audio:
                playSearchArtist("薛之谦");
                //createAlarm("打开创建系统闹钟页面",14,27);
                break;
            case R.id.button_phone:
                callMe("15257135393");
                break;
            case R.id.button_bower:
                openWebPage("http://www.xiaomi.com");
                break;
            default:
                break;
        }
    }
    

    布局文件

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/button_alarm"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="创建闹钟" />
    
        <Button
            android:id="@+id/button_event"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="添加日历" />
    
        <Button
            android:id="@+id/button_ca"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="启动相机" />
    
        <Button
            android:id="@+id/button_cact"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="打开联系人" />
    
        <Button
            android:id="@+id/button_cact_inset"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="插入联系人" />
    
        <Button
            android:id="@+id/button_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="打开电子邮件" />
    
        <Button
            android:id="@+id/button_email_app"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="打开电子邮件" />
    
        <Button
            android:id="@+id/button_cat"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="叫车" />
    
        <Button
            android:id="@+id/button_map"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="打开地图应用" />
    
        <Button
            android:id="@+id/button_audio"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="播放特定歌手音乐" />
        <Button
            android:id="@+id/button_phone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="打电话" />
        <Button
            android:id="@+id/button_bower"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="打开浏览器" />
    
    </LinearLayout>

    相关文章

      网友评论

          本文标题:通用Intent

          本文链接:https://www.haomeiwen.com/subject/ejgyeftx.html