开发一个Android程序,利用它拨打电话和发送短信。
时间: 2024-05-04 17:20:02 浏览: 69
要在Android平台上开发一个能够拨打电话和发送短信的程序,可以使用Android提供的系统API和权限来实现。以下是一个简单的示例代码:
1. 添加权限到AndroidManifest.xml文件中
```xml
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.SEND_SMS" />
```
2. 在Java代码中实现拨打电话和发送短信的功能
```java
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CALL_PHONE = 1;
private static final int REQUEST_SEND_SMS = 2;
private EditText phoneNumberEditText;
private EditText messageEditText;
private Button callButton;
private Button sendMessageButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
phoneNumberEditText = findViewById(R.id.phone_number_edit_text);
messageEditText = findViewById(R.id.message_edit_text);
callButton = findViewById(R.id.call_button);
sendMessageButton = findViewById(R.id.send_message_button);
callButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String phoneNumber = phoneNumberEditText.getText().toString();
Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL_PHONE);
} else {
startActivity(callIntent);
}
}
});
sendMessageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String phoneNumber = phoneNumberEditText.getText().toString();
String message = messageEditText.getText().toString();
SmsManager smsManager = SmsManager.getDefault();
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, REQUEST_SEND_SMS);
} else {
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_CALL_PHONE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
callButton.performClick();
}
} else if (requestCode == REQUEST_SEND_SMS) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
sendMessageButton.performClick();
}
}
}
}
```
这个例子假设你在布局文件中使用了以下控件:
```xml
<EditText
android:id="@+id/phone_number_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone number" />
<EditText
android:id="@+id/message_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Message" />
<Button
android:id="@+id/call_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Call" />
<Button
android:id="@+id/send_message_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send message" />
```
当你点击“Call”按钮时,它会检查是否有拨打电话的权限。如果没有,它会向用户请求该权限。如果用户授予了权限,它会创建一个意图,用于拨打指定的电话号码。
当你点击“Send message”按钮时,它将检查是否有发送短信的权限。如果没有,它会向用户请求该权限。如果用户授予了权限,它将使用SmsManager类发送短信到指定的电话号码。
阅读全文