Android studio实验:备份短信 能运行的实验代码是什么
时间: 2023-07-16 21:12:33 浏览: 101
(android studio)安卓实验查看系统短信
以下是一个简单的备份短信的 Android Studio 实验代码示例:
```java
import android.Manifest;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Telephony;
import android.util.Log;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private static final int PERMISSIONS_REQUEST_READ_SMS = 100;
private static final int PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 101;
private static final String SMS_BACKUP_FILE_NAME = "sms_backup.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Check if we have read SMS permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// Request permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_SMS},
PERMISSIONS_REQUEST_READ_SMS);
} else {
// Permission already granted
backupSms();
}
}
private void backupSms() {
// Check if we have write external storage permission
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// Request permission
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
} else {
// Permission already granted
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(Uri.parse("content://sms/"), null, null, null, null);
if (cursor == null) {
Log.e("SMS Backup", "Cursor is null");
Toast.makeText(this, "Failed to backup SMS", Toast.LENGTH_SHORT).show();
return;
}
try {
File backupFile = new File(Environment.getExternalStorageDirectory(), SMS_BACKUP_FILE_NAME);
FileOutputStream fos = new FileOutputStream(backupFile);
OutputStreamWriter writer = new OutputStreamWriter(fos);
// Write header
writer.write("Address,Date,Type,Body\n");
// Loop through cursor and write to file
while (cursor.moveToNext()) {
String address = cursor.getString(cursor.getColumnIndex(Telephony.Sms.ADDRESS));
long date = cursor.getLong(cursor.getColumnIndex(Telephony.Sms.DATE));
String type = cursor.getString(cursor.getColumnIndex(Telephony.Sms.TYPE));
String body = cursor.getString(cursor.getColumnIndex(Telephony.Sms.BODY));
writer.write(String.format(Locale.getDefault(), "%s,%s,%s,%s\n",
address, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date(date)),
type, body));
}
writer.close();
fos.close();
Toast.makeText(this, "SMS backup completed", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Log.e("SMS Backup", "Failed to backup SMS", e);
Toast.makeText(this, "Failed to backup SMS", Toast.LENGTH_SHORT).show();
} finally {
cursor.close();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSIONS_REQUEST_READ_SMS:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
backupSms();
} else {
Toast.makeText(this, "Read SMS permission required", Toast.LENGTH_SHORT).show();
}
break;
case PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
backupSms();
} else {
Toast.makeText(this, "Write external storage permission required", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
```
代码中使用了两个常量 `PERMISSIONS_REQUEST_READ_SMS` 和 `PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE` 来表示请求权限的请求码,备份短信的文件名为 `SMS_BACKUP_FILE_NAME`。
需要注意的是,该代码中只备份了收到的短信,对于发送的短信需要另行处理。此外,还需要在 AndroidManifest.xml 文件中添加读取短信和写入外部存储器的权限声明:
```xml
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
阅读全文