首先设置清单文件,这个很重要!!!
<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"/>
URL的格式由下列三部分组成:
第一部分是协议(或称为服务方式);
第二部分是存有该资源的主机IP地址(有时也包括端口号);
第三部分是主机资源的具体地址。,如目录和文件名等。
Activity的代码:
package com.jerehedu.jereduch08;
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.util.Log;
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 com.jerehedu.jereduch08.util.FileUitlity;
import com.jerehedu.jereduch08.util.RoundImageView;
import java.io.File;
public class IntentActivity extends AppCompatActivity implements View.OnClickListener {
private Button bt1, bt2, bt3,bt4,bt5,bt6,bt7,bt8,bt11,bt21,bt31;
private PopupWindow pw;
private View popView;
private RoundImageView ri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intent);
//从主布局中取得控件
bt1 = (Button) findViewById(R.id.bt1);
bt2 = (Button) findViewById(R.id.bt2);
bt3 = (Button) findViewById(R.id.bt3);
bt4= (Button) findViewById(R.id.bt4);
bt5= (Button) findViewById(R.id.bt5);
bt6= (Button) findViewById(R.id.bt6);
bt7= (Button) findViewById(R.id.bt7);
bt8= (Button) findViewById(R.id.bt8);
ri= (RoundImageView) findViewById(R.id.ri);
//加载PopWindow的布局
popView=this.getLayoutInflater().inflate(R.layout.pop_layout,null);
//从PopWindow中取得控件
bt11= (Button) popView.findViewById(R.id.bt11);
bt21= (Button) popView.findViewById(R.id.bt21);
bt31= (Button) popView.findViewById(R.id.bt31);
ri.setOnClickListener(this);
//本类监听
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
bt5.setOnClickListener(this);
bt6.setOnClickListener(this);
bt7.setOnClickListener(this);
bt8.setOnClickListener(this);
ri.setOnClickListener(this);
bt11.setOnClickListener(this);
bt21.setOnClickListener(this);
bt31.setOnClickListener(this);
//显示Intent,明确指出要跳转的组件。
//Intent intent=new Intent(IntentActivity.this,MainActivity.class);
//tartActivity(intent);
//---------------------------------
//隐式Intent,有android系统帮助匹配。
//匹配规则: 清单文件中的Intent-filter标签中的action
// Uri uri=Uri.parse("tel:10086111");
//Uri是一个用于标识某一互联网资源名称的字符串。
// 该种标识允许用户对任何(包括本地和互联网)的资源通过特定的协议进行交互操作。
// Intent intent=new Intent(Intent.ACTION_DIAL,uri);
// startActivity(intent);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id) {
case R.id.bt1:
Uri uri = Uri.parse("tel:10086111");
Intent intent = new Intent(Intent.ACTION_CALL, uri);
//ACTION_CALL是危险权限
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 it = new Intent(Intent.ACTION_VIEW);
it.putExtra("sms_body", "代开发票");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
break;
case R.id.bt3:
Uri uri1=Uri.parse("http://www.baidu.com");
Intent it1=new Intent(Intent.ACTION_VIEW,uri1);
startActivity(it1);
break;
case R.id.bt4:
Intent it2=new Intent(Intent.ACTION_VIEW);
File file=new File("/storage/emulated/0/KuwoMusic/music/我好想你.mp3");
it2.setDataAndType(Uri.fromFile(file),"audio/*");
startActivity(it2);
break;
case R.id.bt5:
Intent it3=new Intent(Intent.ACTION_VIEW);
File file1=new File("/sdcard/");
//文件的路径
it3.setDataAndType(Uri.fromFile(file1),"video/*");
startActivity(it3);
break;
case R.id.bt6:
Intent it4=new Intent(Intent.ACTION_VIEW);
File file2=new File("/storage/emulated/0/sina/weibo/weibo/12.jpg");
it4.setDataAndType(Uri.fromFile(file2),"image/*");
startActivity(it4);
break;
case R.id.bt7:
Intent it5=new Intent(Intent.ACTION_VIEW);
it5.setDataAndType(Uri.parse("file:///storage/emulated/0/新建文件夹/Beytagh最佳.apk"),
"application/vnd.android.package-archive");
startActivity(it5);
break;
case R.id.bt8:
notfication();
break;
case R.id.ri://点击头像打开PopWindow
pw = getPopWindow(popView);
break;
case R.id.bt11://调用相机
takephoto();
break;
case R.id.bt21://图库
phonePhoto();
break;
case R.id.bt31:
pw.dismiss();
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);
//调用相机的Action
//设置存储路径
File parent= FileUitlity.getInstance(getApplicationContext())
.makeDir("head_img");
capturePath=parent.getPath()
+File.separatorChar
+System.currentTimeMillis()+".jpg";
Log.d("=====",capturePath);
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) {
Log.d("==requestCode===", String.valueOf(requestCode));
return;
}
Log.d("==requestCode1===", String.valueOf(requestCode));
//相机返回结果,调用系统裁剪
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(); // cursor相当于指针
startPicZoom(Uri.fromFile(new File(capturePath)));
}else if (requestCode==3){
Bundle bundle= data.getExtras();
if (bundle!=null){
Bitmap bitmap= bundle.getParcelable("data");
ri.setImageBitmap(bitmap);
}
pw.dismiss();
}
}
/*
调用系统裁剪功能
*/
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 notfication(){
//先定义一个Intent
Intent intent=new Intent(this,MainActivity.class);
//使用PendingIntent 封装 Intent
/*PendingIntent 的第四个参数的说明
常量:FLAG_CANCEL_CURRENT生成一个新的对象
FLAG_NO_CANCEL 如果不存在,则创建一个新的对象
FLAG_ONE_SHORT 创建的对象只是用一次
FLAG_UPDATE_CURRENT 已存在则直接使用
*/
PendingIntent pi=PendingIntent.getActivities(
this,0,new Intent[]{intent},
PendingIntent.FLAG_UPDATE_CURRENT);
//获取通知服务
NotificationManager nm= (NotificationManager) getSystemService
(Activity.NOTIFICATION_SERVICE);
Notification nf=new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher).setTicker("A")
.setContentInfo("我是通知栏信息")
.setContentText("奥运会")
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pi)
.build();
//通过通知服务,显示通知。0是id,不同的通知栏有不同的id否则将会被覆盖
nm.notify(0,nf);
}
//
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.popStyle);
//设置背景透明度
// WindowManager.LayoutParams ll=getWindow().getAttributes();
// ll.alpha=0.6f;
// getWindow().setAttributes(ll);
backgroundAlpha(0.3f);
popupWindow.setBackgroundDrawable(new ColorDrawable());
//不设置背景,无法退出
popupWindow.showAtLocation(ri, Gravity.BOTTOM,0,0);
//第一个参数只是参照物,页面内的控件都可
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
// WindowManager.LayoutParams ll = getWindow().getAttributes();
// ll.alpha = 1f;
// getWindow().setAttributes(ll);
backgroundAlpha(1.0f);
}
});
return popupWindow;
}
public void backgroundAlpha(float bgAlpha){
WindowManager.LayoutParams lp=getWindow().getAttributes();
lp.alpha=bgAlpha;
getWindow().setAttributes(lp);
}
}
主布局文件中:
<?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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jerehedu.jereduch08.IntentActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt1"
android:text="拨打电话"
android:textSize="20sp"
android:layout_above="@+id/bt2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt2"
android:text="发送短信"
android:textSize="20sp"
android:layout_above="@+id/bt3"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt3"
android:text="打开网页"
android:textSize="20sp"
android:layout_above="@+id/bt4"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt4"
android:text="播放音乐"
android:textSize="20sp"
android:layout_above="@+id/bt5"
android:layout_alignParentStart="true" />
android:layout_marginTop="44dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt5"
android:text="播放视频"
android:textSize="20sp"
android:layout_above="@+id/bt6"
android:layout_alignParentStart="true"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt6"
android:text="浏览图片"
android:textSize="20sp"
android:layout_above="@+id/bt7"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt7"
android:text="安装程序"
android:textSize="20sp"
android:layout_above="@+id/bt8"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt8"
android:text="通知栏消息"
android:textSize="20sp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true" />
<com.jerehedu.jereduch08.util.RoundImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/ri"
android:src="@mipmap/f1" />
</RelativeLayout>
引用PopWindow:--PopWindow中的布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt11"
android:text="拍照"
android:textSize="16sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt21"
android:text="从手机相册选择"
android:textSize="16sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt31"
android:text="取消"
android:textSize="16sp"/>
</LinearLayout>
引用工具代码:
FileUitlity::作用:可创建文件夹和目录等路径
IntentActivity: 文件里包含PopWindow---需要包含两个anim内的文件enter-pop exit-pop.
PopWindow中动画弹出的效果:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="popStyle">
<!--window进入-->
<item name="android:windowEnterAnimation">
@anim/enter_pop
</item>
<!--window退出-->
<item name="android:windowExitAnimation">
@anim/exit_pop
</item>
</style>
</resources>
enter_pop:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--透明度-->
<alpha android:fromAlpha="0.5"
android:toAlpha="1"
android:duration="500">
</alpha>
<!--位置-->
<translate android:fromYDelta="100%"
android:toYDelta="0"
android:duration="500">
</translate>
</set>
exit_pop:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="1"
android:toAlpha="0.5"
android:duration="500">
</alpha>
<translate android:fromYDelta="0"
android:toYDelta="100%"
android:duration="500">
</translate>
</set>
RoundImageView:设置图片的圆形形状
点击任意一个按钮 ,即可出现与按钮名称所对应的功能
点击头像,弹出PopWindow对话框,完成手机照相的功能,并且可直接修改头像。