package com.jy.day7intenthiddenjump;
import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button mBnt1;
/**
* 隐式跳转-启动本app中的activity
*/
private Button mBnt2;
/**
* 隐式跳转启动打电话功能
*/
private Button mBnt3;
/**
* 隐式跳转启动发短信功能
*/
private Button mBnt4;
/**
* 隐式跳转启动相册
*/
private Button mBnt5;
private ImageView mImg;
/**
* 隐式跳转拍照
*/
private Button mBnt6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mBnt1 = (Button) findViewById(R.id.bnt1);
mBnt1.setOnClickListener(this);
mBnt2 = (Button) findViewById(R.id.bnt2);
mBnt2.setOnClickListener(this);
mBnt3 = (Button) findViewById(R.id.bnt3);
mBnt3.setOnClickListener(this);
mBnt4 = (Button) findViewById(R.id.bnt4);
mBnt4.setOnClickListener(this);
mBnt5 = (Button) findViewById(R.id.bnt5);
mBnt5.setOnClickListener(this);
mBnt6 = (Button) findViewById(R.id.bnt6);
mBnt6.setOnClickListener(this);
mImg = (ImageView) findViewById(R.id.img);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.bnt1:
btn1();
break;
case R.id.bnt2:
btn2();
break;
case R.id.bnt3:
callPhone();
break;
case R.id.bnt4:
sms();
break;
case R.id.bnt5:
picture();
break;
case R.id.bnt6:
// camera();//相机拍照
openBrower();
break;
}
}
//打开所有app,自行选中
private void openBrower() {
Intent intent = new Intent(Intent.ACTION_VIEW);
startActivity(intent);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == RESULT_OK) {//相册的返回
Uri uri = data.getData();//得到选中的图片的Uri路径
mImg.setImageURI(uri);//使用图片
}else if(requestCode == 200 && resultCode == RESULT_OK){
Bitmap bt = (Bitmap) data.getExtras().get("data");
mImg.setImageBitmap(bt);
}
}
private void camera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,200);
}
private void picture() {
//Intent.ACTION_PICK 相册的隐式action MediaStore.Images.Media.EXTERNAL_CONTENT_URI相册路径
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 100);//启动相册,选中图片后,系统把选择的图片回传给我们
}
//发短信
private void sms() {
Uri uri = Uri.parse("smsto:10086");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
startActivity(intent);
}
//掉出打电话
private void callPhone() {
Uri uri = Uri.parse("tel:15225692907");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);//隐式跳转到打电话app,同时传值
startActivity(intent);
}
private void btn1() {
//启动另一个app
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.jy.day7activitycallback",
"com.jy.day7activitycallback.MainActivity"));
startActivity(intent);
}
private void btn2() {
//换为隐式跳转启动
Intent intent = new Intent("aaa");
startActivity(intent);
}
}s