您的位置:首页 > 移动开发 > Android开发

Android----Intent,运用由android系统帮助匹配实现打电话、发送短信、打开网页、播放音乐、打开视频、打开图片、安装APK、通知栏消息、拍照上传头像等功能

2016-08-10 22:19 1341 查看
Intent 分为两种:显式和隐式

显示Intent:明确指定要跳转的组件为

如: Intent intent=new Intent(MainActivity.this,SecondActivity.class);

隐式Intent: 由android系统帮助匹配,匹配规则:在清单文件由intent-filter 标签中的action进行调用匹配。

在清单文件中添加权限

<!--拨打电话的权限-->
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<!--链接网络的权限-->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<!--读写文件的权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>


总体效果图





实现程序需要帮助类,来实现存储图片,导出图片,以及将头像变成圆形等功能。将需要的两个工具类放入util中

以下代码是实现实现打电话、发送短信、打开网页、播放音乐、打开视频、打开图片、安装APK、通知栏消息、拍照上传头像等功能。运用POPWINDOW实现弹出窗口。

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private  RoundImageView riv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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中的控件
View view=getLayoutInflater().inflate(R.layout.popwindow,null);
Button btn1 = (Button) view.findViewById(R.id.btn1);
Button btn3 = (Button) view.findViewById(R.id.btn3);
//注册本类监听
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
bt5.setOnClickListener(this);
bt6.setOnClickListener(this);
bt7.setOnClickListener(this);
bt8.setOnClickListener(this);
btn1.setOnClickListener(this);      view.findViewById(R.id.btn2).setOnClickListener(this);
final PopupWindow pw=new PopupWindow(view);
pw.setFocusable(true);
ColorDrawable sd=new ColorDrawable();
pw.setBackgroundDrawable(sd);
riv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int width = getResources().getDisplayMetrics().widthPixels;
pw.setWidth(width);
int height =getResources().getDisplayMetrics().heightPixels/2;
pw.setHeight(height);
pw.showAtLocation(view, Gravity.BOTTOM, 0, 0);
WindowManager.LayoutParams ll = getWindow().getAttributes();
ll.alpha = 0.3f;
getWindow().setAttributes(ll);

}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pw.dismiss();
WindowManager.LayoutParams ll=getWindow().getAttributes();
ll.alpha=1f;
getWindow().setAttributes(ll);
}
});
}

@Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.bt1:
Uri uri1 = Uri.parse("tel:18865557601");
Intent intent = new Intent(Intent.ACTION_CALL, uri1);
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 file1 =new File("/storage/emulated/0/goldfallen.mp3");
it3.setDataAndType(Uri.fromFile(file1),"audio/*");
startActivity(it3);
break;
case R.id.bt5:
Intent it4=new Intent(Intent.ACTION_VIEW);
File file2 =new File("/sdcard/music.mp3");
it4.setDataAndType(Uri.fromFile(file2),"video/*");
startActivity(it4);
break;
case R.id.bt6:
Intent it5=new Intent(Intent.ACTION_VIEW);
File file3 =new File("/storage/emulated/0/news_article/a.jpg");
it5.setDataAndType(Uri.fromFile(file3),"image/*");
startActivity(it5);
break;
//利用这段代码就可以让下载下的apk进行安装
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);
break;
case R.id.bt8:
Intent it7=new Intent(Intent.ACTION_VIEW);
notification();
startActivity(it7);
break;
case R.id.btn1:
//调用相机
takePhoto();
break;
case R.id.btn2:
phonePhoto();
break;

}
}
private String capturePath="";
//调用相机
public void takePhoto(){
//调用相机的action
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//ApplicationContext()最不容易被回收
File parent = FileUitlity.getInstance(getApplicationContext())
.makeDir("head_img");
// Log.d("==j==",parent);
//separatorChar斜杠
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带请求码的
startActivityForResult(camera,1);
}
//调用图库
public void phonePhoto(){
Intent intent=new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
}
//调用系统裁剪功能
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){
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);
}
}
}

//消息栏通知
public void notification(){
//先定义一个Intent
Intent intent = new Intent(this,SecondActivity.class);
//使用PendingIntent封装intent
/*PendingIntent的四个参数说明
常量:FLAG_CANCEL_CURRENT生成一个新的对象
FLAG_NO_CREATE如果不存在,则创建一个新的对象
FLAG_ONE_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)
.setContentInfo("A").setContentInfo("我是通知栏消息")
.setContentTitle("奥运会")
.setContentText("PendingIntent的使用方法")
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setContentIntent(pi)
.build();
//通知服务显示通知
nm.notify(0, notification);
}
}


自然需要布局文件,一个主页面布局文件,一个POPWINDOW布局文件

主页面布局文件:activity_main.xml

<?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.edu.jereh.android8.MainActivity">

<com.edu.jereh.android8.util.RoundImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:id="@+id/riv"
android:src="@mipmap/lh1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt1"
android:text="拨打电话"
android:layout_below="@id/riv"

/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt2"
android:text="发送短信"
android:layout_below="@id/bt1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt3"
android:layout_below="@id/bt2"
android:text="打开网页"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt4"
android:layout_below="@id/bt3"
android:text="播放音乐"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt5"
android:layout_below="@id/bt4"
android:text="打开MP4"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt6"
android:layout_below="@id/bt5"
android:text="打开图片"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt7"
android:layout_below="@id/bt6"
android:text="安装Apk"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/bt8"
android:layout_below="@id/bt7"
android:text="通知栏消息"
/>
</RelativeLayout>


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:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="打开相机"
/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="打开相册"
/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:text="取消"
/>
</LinearLayout>


当然还可以加入选择器,对边框进行优化

以下为部分具体实现效果图

点击头像



选取图片或者照照片进行截取图片



头像更换成功



发送短信功能





通知栏消息

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐