今天第10天瞜:)今天学习显性intent和隐性intent还有向下一个Activity发送数据

利用intent.setAction去呼叫手机里的应用例如:电话、简讯等等

public class ActionUriActivity extends AppCompatActivity implements View.OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_action_uri);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
findViewById(R.id.btn_dial).setOnClickListener(this);
findViewById(R.id.btn_sms).setOnClickListener(this);
findViewById(R.id.btn_my).setOnClickListener(this);
}

@Override
public void onClick(View view) {
Intent intent = new Intent();
String phoneNo = "55688";
if(view.getId() == R.id.btn_dial){
intent.setAction(intent.ACTION_DIAL);
Uri uri = Uri.parse("tel:"+ phoneNo);
intent.setData(uri);
startActivity(intent);
} else if (view.getId() == R.id.btn_sms) {
intent.setAction(intent.ACTION_SENDTO);
Uri uri = Uri.parse("smsto:"+ phoneNo);
intent.setData(uri);
startActivity(intent);
}else if(view.getId() == R.id.btn_my){
intent.setAction("android.intent.action.NING");
intent.addCategory(intent.CATEGORY_DEFAULT);
startActivity(intent);
}
}
}

写两个Activity来传递数据

public class ActSendActivity extends AppCompatActivity implements View.OnClickListener {

private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_act_send);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
findViewById(R.id.btn_send).setOnClickListener(this);
textView = findViewById(R.id.tv_send);
}

@Override
public void onClick(View view) {
Intent intent = new Intent(this, ActReceiveActivity.class);
Bundle bundle = new Bundle();
bundle.putString("现在时间", DateUtil.getNowTime());
bundle.putString("天气",textView.getText().toString());
intent.putExtras(bundle);
startActivity(intent);
}
}

public class ActReceiveActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_act_receive);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
TextView textView = findViewById(R.id.tv_receive);
Bundle bundle = getIntent().getExtras();
String time = bundle.getString("现在时间");
String weather = bundle.getString("天气");
//textView.setText("现在时间"+time+"天气"+weather);
String desc = String.format("现在时间:%s\\n天气%s",time,weather);
textView.setText(desc);

}
}