intent:除启动新的Activity这一作用外还有作为传递数据和事件的桥梁的作用用于启用手机中的一些应用
完成的效果:拨打电话、发送短信、打开网页、播放音乐、播放视频、浏览图片、安装APK、通知栏消息
popwindow中:拍照、图片裁剪并设置成当前头像、取消键
主代码
package com.edu.jereh.jreduch09; 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.PopupWindow; import java.io.File; public class IntentActivity extends AppCompatActivity implements View.OnClickListener { private Button bt9; private PopupWindow pw; private View v; RoundImageView riv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intent); bt9 = (Button) findViewById(R.id.bt9); v = getLayoutInflater().inflate(R.layout.popwindow,null); pw = new PopupWindow(v); pw.setFocusable(true); ColorDrawable cd = new ColorDrawable(); pw.setBackgroundDrawable(cd); //backgroundAlpha(0.3f);//设置屏幕背景透明度 pw.setAnimationStyle(R.style.pop); bt9.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ WindowManager.LayoutParams ll = getWindow().getAttributes();//设置背景透明度 ll.alpha = 0.6f; getWindow().setAttributes(ll); pw.setWidth(view.getWidth()); int height = getResources().getDisplayMetrics().heightPixels/2; pw.setHeight(height); pw.showAtLocation(view, Gravity.BOTTOM,0,0); } }); pw.setOnDismissListener(new PopupWindow.OnDismissListener() { //消失的时候的监听 @Override public void onDismiss() { WindowManager.LayoutParams ll = getWindow().getAttributes(); ll.alpha = 1.0f; getWindow().setAttributes(ll); } }); 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); Button xx = (Button) v.findViewById(R.id.xx); Button yy = (Button) v.findViewById(R.id.yy); Button zz = (Button) v.findViewById(R.id.zz);//注册本类监听 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); xx.setOnClickListener(this); yy.setOnClickListener(this); zz.setOnClickListener(this); // Intent intent = new Intent(IntentActivity.this,SecondActivity.class); // startActivity(intent);//显式intent,明确指定要跳转的组件 // Uri uri1 = Uri.parse("tel.1008611"); // Intent intent = new Intent(Intent.ACTION_DIAL,uri1); // startActivity(intent); //隐式intent 由Android系统去匹配打开什么 动作请求 //匹配规则 清单文件中的 Intent-filter标签中的action } @Override public void onClick(View v) { int id = v.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.手机百度.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("/Music/Lydia.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("/qqmusic/mv/于文文-心跳(Live)[mqms].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("/DCIM/P50702-164843.jpg");//找到文件路径 it5.setDataAndType(Uri.fromFile(file2), "image/*"); startActivity(it5); break; case R.id.bt7: Intent it6 = new Intent(Intent.ACTION_VIEW); it6.setDataAndType(Uri.parse("file:///sdcard/qq.apk"), "application/vnd.android.package-archive"); startActivity(it6);//安装apk break; case R.id.bt8: notification(); break; case R.id.xx: takePhoto(); break; case R.id.yy: phonePhoto(); break; case R.id.zz: pw.dismiss(); break; } } 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); } 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()+".jpg"; camera.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(capturePath))); camera.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1); startActivityForResult(camera, 1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(resultCode!=Activity.RESULT_OK){ return; } if(requestCode == 1){ //相机返回结果,调用系统裁剪 startPicZoom(Uri.fromFile(new File(capturePath))); return; }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); } } } public void notification(){ //消息栏通知 Intent intent = new Intent(this,SecondActivity.class);//先定义一个Intent PendingIntent pi = PendingIntent.getActivities(this,0,new Intent[]{intent},PendingIntent.FLAG_UPDATE_CURRENT); //使用PendingIntent封装Intent /*PendingIntent的第四个参数说明: 常量:FLAG_CANCLE_CURRENT生成一个新的对象 FLAG_NO_CREATER 如果不存在。则创建一个新的对象 FLAG_ONE_SHOT 创建的对象只是用一次 FLAG_UPDATE_CURRENT 已存在则直接使用 */ NotificationManager nm = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);//获取通知服务 Notification notification = new NotificationCompat.Builder(this)//构建一个通知 .setSmallIcon(R.mipmap.lao) .setTicker("A") .setContentInfo("我是通知栏消息") .setContentTitle("奥运会") .setContentText("PendingIntent的使用方法") .setAutoCancel(true) .setDefaults(Notification.DEFAULT_ALL) .setContentIntent(pi) .build(); nm.notify(0,notification);//通过通知服务 显示通知 0是通知栏的ID,再写一个通知栏ID不变的话会覆盖之前的,要改ID } }
布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.edu.jereh.jreduch09.IntentActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt1" android:layout_marginTop="50dp" android:text="拨打电话" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt2" android:layout_below="@+id/bt1" android:layout_marginTop="5dp" android:text="发送短信"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/bt2" android:id="@+id/bt3" android:text="打开网页" android:layout_marginTop="5dp" android:layout_alignParentStart="true" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt4" android:text="播放音乐" android:layout_marginTop="5dp" android:layout_below="@+id/bt3"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt5" android:text="播放视频" android:layout_marginTop="5dp" android:layout_below="@+id/bt4"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt6" android:text="浏览图片" android:layout_marginTop="5dp" android:layout_below="@+id/bt5"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt7" android:text="安装APK" android:layout_marginTop="5dp" android:layout_below="@+id/bt6"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt8" android:text="通知栏消息" android:layout_marginTop="5dp" android:layout_below="@+id/bt7"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/bt9" android:layout_marginTop="3dp" android:layout_below="@+id/bt8" android:text="popwindow"/> <com.edu.jereh.jreduch09.RoundImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:id="@+id/riv"/> </RelativeLayout>popwindow布局文件<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#ecf6f7" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="300dp" android:layout_height="wrap_content" android:text="拍照" android:layout_marginLeft="45dp" android:gravity="center" android:layout_marginTop="20dp" android:textSize="30sp" android:id="@+id/xx"/> <Button android:layout_width="300dp" android:layout_height="wrap_content" android:text="从手机相册选择" android:layout_marginLeft="45dp" android:layout_marginTop="20dp" android:textSize="30sp" android:id="@+id/yy"/> <Button android:layout_width="300dp" android:layout_height="wrap_content" android:text="取消" android:layout_marginLeft="45dp" android:layout_marginTop="20dp" android:textSize="30sp" android:id="@+id/zz"/> </LinearLayout>popwindow载入退出时的动画效果代码载入:<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1000" android:fromYDelta="100%p" android:toYDelta="0"/> <alpha android:fromAlpha="1.0" android:toAlpha="0.5"/> </set>退出:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="1000" android:fromYDelta="0" android:toYDelta="100%p"/> <alpha android:fromAlpha="0.5" android:toAlpha="1.0"/> </set>
在清单文件中加入权限
<uses-permission android:name="android.permission.CALL_PHONE"/> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!--获取一些权限:依次是打电话、连接网络、读、写文件-->
效果展示(网页、音乐、视频、图片显示的是选择手机中相应的应用打开,安装APK由于为下载显示解析包错误,点击通知栏消息时会有震动和音效下拉屏幕后出现点击后设计了一个监听、画面跳转)