Android studio case-realize phone call, SMS sending, camera, map function
Overview
Design an APP to realize the functions of phone call, SMS sending, camera, and map functions.
The phone dialing function is not introduced here, you can refer to this article:
Android studio case-implement phone dialing .
1. SMS sending
1. Layout
.xml file
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="电话号码"
android:inputType="number"
android:id="@+id/num_btn"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="短信内容"
android:id="@+id/mes_btn"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="发送"
android:textColor="#ff0066"
android:id="@+id/send_btn"/>
2. Code
.java file
public class message extends AppCompatActivity {
EditText num_btn;
EditText mes_btn;
Button send_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
num_btn=(EditText) findViewById(R.id.num_btn);
mes_btn=(EditText) findViewById(R.id.mes_btn);
send_btn=(Button) findViewById(R.id.send_btn);
send_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent();
String strPhoneNum = num_btn.getText().toString();
Uri uri = Uri.parse("smsto:"+strPhoneNum);
String strMsg = mes_btn.getText().toString();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(uri);
intent.putExtra("sms_body", strMsg);
startActivity(intent);
}
});
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
3. Demo
Second, the camera is turned on
1. Code
The system camera is called, so there is no layout.
.java
case R.id.type3_btn:
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
break;
2. Demo
3. Map
1. Code
case R.id.type4_btn:
Uri uri = Uri.parse("geo:39.9,116.3");
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
break;
2. Demo
Fourth, the total interface
1. Layout
.xml file
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="电话"
android:textColor="#ff0066"
android:background="#00FFF7"
android:id="@+id/type1_btn"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="短信"
android:textColor="#00aaff"
android:background="#00ff00"
android:id="@+id/type2_btn"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="相机"
android:textColor="#899ad5"
android:background="#ff00ff"
android:id="@+id/type3_btn"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="地图"
android:textColor="#00ff00"
android:background="#ff0066"
android:id="@+id/type4_btn"/>
2. Code
.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button type1_btn;
Button type2_btn;
Button type3_btn;
Button type4_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
type1_btn=(Button) findViewById(R.id.type1_btn);
type2_btn=(Button) findViewById(R.id.type2_btn);
type3_btn=(Button) findViewById(R.id.type3_btn);
type4_btn=(Button) findViewById(R.id.type4_btn);
type1_btn.setOnClickListener(this);
type2_btn.setOnClickListener(this);
type3_btn.setOnClickListener(this);
type4_btn.setOnClickListener(this);
}
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
@Override
public void onClick(View v) {
Intent intent=new Intent();
switch (v.getId()){
case R.id.type1_btn:
intent.setClassName("com.example.phone","com.example.phone.MainActivity");
break;
case R.id.type2_btn:
intent.setClassName(this,"com.example.menu.message");
break;
case R.id.type3_btn:
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
break;
case R.id.type4_btn:
Uri uri = Uri.parse("geo:39.9,116.3");
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
break;
}
startActivity(intent);
}
}