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

关于手机应用里的打电话,发短信,拍照等

2016-08-10 20:13 295 查看
我们都知道现在的app几乎每个都能直接打电话,发短信,启动照相机等等,今天我们来讲一讲如何用程序来实现这些功能的。

程序如下:

package com.edu.jereh.android8;

//导入我们需要的包

//差不多都是自动导入的

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 com.edu.jereh.android8.util.FileUitlity;

import com.edu.jereh.android8.util.RoundImageView;

import java.io.File;

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);

//明确指定要跳转的组件为显示Intent
//Intent intent=new Intent(MainActivity.this,SecondActivity.class);
//隐式Intent 由android系统帮助匹配,匹配规则:在清单文件由intent-filter 标签中的action


// Uri uri=Uri.parse(“tel:1008611”);

// Intent intent=new Intent(Intent.ACTION_DIAL,uri);

// startActivity(intent);

//注册本类监听

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:
//----------------------------------------
//隐式Intent由Anroid系统帮助匹配
//匹配规则  清单文件中的Intent—filter  标签中的action
Uri uri1 = Uri.parse("tel:15653510512");
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("/sdcard/mp3.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("/sdcard/mp3/c.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;

}
}
//调用系统裁剪功能
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);

}
private String capturePath="";
//调用相机
public void takePhoto(){
//调用相机的action
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//ApplicationContext()最不容易被回收
File parent = FileUitlity.getInstance(getApplicationContext())
.makeDir("head_img");
//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);
}

@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);
}


}

今天这个可以当成模板的,有想用的可以直接用就可以了,

友情提示:不要把手机号发到网上。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  手机 app