今天第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);
}
}