了解隐示Intent跳转方法

以下主要介绍了关于隐示Intent的使用方法:

       1、在Activity布局中添加了8个Button控件,分别为拨打电话、发送短信、打开网页、播放音乐、播放视频、播放图片、安装apk和通知栏等内容,代码见下面:

       2、在布局中添加了一个ImageView控件调用RoundImageView使得图片为圆形,

       3、对ImageView控件进行监听,使用PopWindow在点击图片后弹出一个PopWindow

       4、对弹出的PopWindow设置:重建一个layout,在layout中添加三个Button控件,分别为拍照、从手机相册中选择、取消,再对三个控件进行监听,从而实现了拍照功能,获取图片和剪裁图片的功能

 

 

 

 

package com.example.administrator.jreduch06;

import android.Manifest;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import java.io.File;

public class IntentActivity extends AppCompatActivity
        implements View.OnClickListener {
    private RoundImageView riv;
    private View popView;
    private PopupWindow pw;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_intent);
        //从主控局中取得控件
        Button bt1 = (Button) findViewById(R.id.bt1);
        Button bt2 = (Button) findViewById(R.id.bt2);
        Button bt3 = (Button) findViewById(R.id.bt3);
        Button bt4 = (Button) findViewById(R.id.bt4);
        Button bt5 = (Button) findViewById(R.id.bt5);
        Button bt6 = (Button) findViewById(R.id.bt6);
        Button bt7 = (Button) findViewById(R.id.bt7);
        Button bt8 = (Button) findViewById(R.id.bt8);
        riv = (RoundImageView)findViewById(R.id.riv);
        //加载popWindow的布局
        popView=this.getLayoutInflater().inflate(R.layout.popwindow,null);
        //从popWindow中取得控件
        Button cancle= (Button) popView.findViewById(R.id.cancle);
        Button camera= (Button) popView.findViewById(R.id.camera);
        Button photo= (Button) popView.findViewById(R.id.photo);
        //注册本类监听
        bt1.setOnClickListener(this);
        bt2.setOnClickListener(this);
        bt3.setOnClickListener(this);
        bt4.setOnClickListener(this);
        bt5.setOnClickListener(this);
        bt6.setOnClickListener(this);
        bt7.setOnClickListener(this);
        bt8.setOnClickListener(this);
        riv.setOnClickListener(this);
        cancle.setOnClickListener(this);
        camera.setOnClickListener(this);
        photo.setOnClickListener(this);
/*
        //明确指定要跳转的组件,叫做显示Intent
//        Intent intent=new Intent(IntentActivity.this,SecondActivity.class);
//        startActivity(intent);
        //隐示Intent,由安卓系统帮助匹配
        //匹配规则,清单文件中的Intent-filter标签中的Action
//        Uri uri=Uri.parse("tel:1008611");
//        Intent intent=new Intent(Intent.ACTION_DIAL,uri);
//        startActivity(intent);*/




    }

    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            //拨打电话
            case R.id.bt1:
                Uri uri = Uri.parse("tel:1008611");
                Intent intent = new Intent(Intent.ACTION_CALL, uri);
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
                break;
            //发送短信
            case R.id.bt2:
                Intent it1 = new Intent(Intent.ACTION_VIEW);
                it1.putExtra("sms_body", "代开发票");
                it1.setType("vnd.android-dir/mms-sms");
                startActivity(it1);
                break;
            //打开网页
            case  R.id.bt3:
                Uri uri2=Uri.parse("http://www.baidu.com");
                Intent it2=new Intent(Intent.ACTION_VIEW,uri2);
                startActivity(it2);
                break;
            //播放音乐
            case  R.id.bt4:
                Intent it3=new Intent(Intent.ACTION_VIEW);
                File file=new File("/sdcard/???+-+?????.mp3");
                it3.setDataAndType(Uri.fromFile(file),"audio/*");
                startActivity(it3);
                break;
            //播放视频
            case  R.id.bt5:
                Intent it4=new Intent(Intent.ACTION_VIEW);
                File file1=new File("/sdcard/20140417_221118.mp4");
                it4.setDataAndType(Uri.fromFile(file1),"video/*");
                startActivity(it4);
                break;
            //播放图片
            case  R.id.bt6:
                Intent it5=new Intent(Intent.ACTION_VIEW);
                File file2=new File("/sdcard/f2.png");
                it5.setDataAndType(Uri.fromFile(file2),"image/*");
                startActivity(it5);
                break;
            //安装apk
            case R.id.bt7:
                Intent it6=new Intent(Intent.ACTION_VIEW);
                it6.setDataAndType(Uri.parse("file:///sdcard/Beytagh.apk"),//file路径
                        "application/vnd.android.package-archive");
                startActivity(it6);
                break;
            //通知栏消息
            case R.id.bt8:
                notification();
                break;
            //填充PopWindow
            case R.id.riv:
                pw=getPopWindow(popView);
                break;
            //点击取消后PopWindow退出
            case R.id.cancle:
                pw.dismiss();
                break;
            //照相功能
            case R.id.camera:
                takePhoto();
                break;
            //获取图片
            case R.id.photo:
                phonePhoto();
                break;


        }
    }
    /*
    * 调用图库*/
    public void phonePhoto(){
        Intent intent=new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 2);

    }
    /*
    * 调用相机*/
    private String capturePath="";//文件的路径
    public void takePhoto(){
        Intent camera=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File parent=FileUitlity.getInstance(getApplicationContext())
                .makeDir("head_img");
        capturePath=parent.getPath()
                +File.separatorChar//斜杠
                +System.currentTimeMillis()
                +".jpj";
        camera.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(new File(capturePath)));
        camera.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
        startActivityForResult(camera, 1);

    }
    /*
    * 调用系统的裁剪功能
    * */
    public void startPicZoom(Uri uri){
        Intent intent=new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri,"image/*");
        intent.putExtra("crop", "true");//允许裁剪
        //设置裁剪比例
        intent.putExtra("aspectX",1);
        intent.putExtra("aspectY",1);
        //设置图片的高度宽度
        intent.putExtra("outputX",150);
        intent.putExtra("outputY", 150);

        intent.putExtra("return-data",true);
        startActivityForResult(intent,3);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode!=Activity.RESULT_OK){//返回-1
            return;

        }
        //相机返回结果,调用系统裁剪
        if(requestCode==1){
            startPicZoom(Uri.fromFile(new File(capturePath)));

        }
        //相册返回的结果,调用系统裁剪
        else if(requestCode==2){
            Cursor cursor=getContentResolver()
                    .query(data.getData(),
                            new String[]{MediaStore.Images.Media.DATA},
                            null,null,null);
            cursor.moveToFirst();
            String capturePath=
                    cursor.getString(
                            cursor.getColumnIndex(
                                MediaStore.Images.Media.DATA));
            cursor.close();
            startPicZoom(Uri.fromFile(new File(capturePath)));

        }else if(requestCode==3){
         Bundle bundle= data.getExtras();
            if(bundle!=null){
                 Bitmap bitmap=bundle.getParcelable("data");
                riv.setImageBitmap(bitmap);
            }
            pw.dismiss();
        }
    }


    //消息栏通知
    public void notification(){
        //先定义一个Intent
        Intent  intent=new Intent(this,SecondActivity.class);
        //使用PendingIntent 封装 Intent
        /*//常量FLAG_UPDATE_CURRENT 生成一个新的对象
        *   FLAG_NO_CREATE如果不存在,创建一个新的对象
        *  FLAG_NO_SHOT 创建的对象只使用一次
         *  FLAG_UPDATE_CURRENT 已存在则直接使用*/

        PendingIntent pi=PendingIntent.getActivities(
                this,0,new Intent[]{intent},PendingIntent.FLAG_UPDATE_CURRENT);
        //获取通知服务
        NotificationManager nm=
                (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);
        //构建一个通知
        Notification notification=new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("A")
                .setContentInfo("我是通知栏")
                .setContentTitle("奥运会")
                .setContentText("PendingIntent的使用方法")
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentIntent(pi)
                .build();
        //通过通知服务 显示通知
        nm.notify(0,notification);


    }
/*
* 构建一个popWindow*/
    public PopupWindow getPopWindow(View view) {
        PopupWindow popupWindow=new PopupWindow(view,
                LinearLayout.LayoutParams.MATCH_PARENT,//宽匹配屏幕,高匹配内容
                LinearLayout.LayoutParams.WRAP_CONTENT,
                true);
        //获取光标
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);//点击外面是否消失
        popupWindow.setAnimationStyle(R.style.pop);//设置动画效果,在style中设置动画效果
        backgroundAlpha(0.3f);//设置背景透明度
        popupWindow.setBackgroundDrawable(new ColorDrawable());//背景色
        popupWindow.showAtLocation(riv, Gravity.BOTTOM, 0, 0);//显示位置
        //当popWindow消失时背景色的b
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1.0f);
                riv.setVisibility(View.VISIBLE);
            }
        });
        return popupWindow;
    }
    //设置背景透明度
    public void backgroundAlpha(float bgAlpha){
        WindowManager.LayoutParams ll=getWindow().getAttributes();
        ll.alpha=bgAlpha;
        getWindow().setAttributes(ll);

    }


}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值