import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Permission mPermission = new Permission(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button permission = findViewById(R.id.permission);
permission.setOnClickListener(this);
Button show = findViewById(R.id.show);
show.setOnClickListener(this);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.permission:
//Dynamic permissions are required to read information
mPermission.requestPermissions();
break;
case R.id.show:
//Scrolling display
TextView tv = new TextView(this);
tv.setText(getSmsInPhone());
ScrollView sv = new ScrollView(this);
sv.addView(tv);
setContentView(sv);
break;
default:
break;
}
}
@SuppressLint("Recycle")
public String getSmsInPhone() {
//The message to access the URL
final String SMS_ALL = "content://sms/";
final String SMS_INBOX = "content://sms/inbox";
final String SMS_SEND = "content://sms/sent";
final String SMS_DRAFT = "content://sms/draft";
final String SMS_OUTBOX = "content://sms/outbox";
final String SMS_FAILED = "content://sms/failed";
final String SMS_QUEUED = "content://sms/queued";
Cursor cursor = null;
StringBuilder smsBuilder = new StringBuilder();
try {
Uri uri = Uri.parse(SMS_INBOX);
String[] projection = new String[] { "_id", "address", "person", "body", "date", "type" };
cursor = getContentResolver().query(uri, projection, null, null, "date desc");
if (cursor != null) {
if (cursor.moveToFirst()) {
// get internal SMS
int indexAddress = cursor.getColumnIndex("address");
int indexPerson = cursor.getColumnIndex("person");
int indexBody = cursor.getColumnIndex("body");
int indexDate = cursor.getColumnIndex("date");
int indexType = cursor.getColumnIndex("type");
do {
String strAddress = cursor.getString(indexAddress);
int intPerson = cursor.getInt(indexPerson);
String strBody = cursor.getString(indexBody);
long longDate = cursor.getLong(indexDate);
int intType = cursor.getInt(indexType);
// format time
@SuppressLint("SimpleDateFormat")
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
Date d = new Date(longDate);
String strDate = dateFormat.format(d);
// format the content
String strType = "";
if (intType == 1) {
strType = "receive";
} else if (intType == 2) {
strType = "send";
} else {
strType = "null";
}
smsBuilder.append("[ ");
smsBuilder.append(strAddress).append(", ");
smsBuilder.append(intPerson).append(", ");
smsBuilder.append(strBody).append(", ");
smsBuilder.append(strDate).append(", ");
smsBuilder.append(strType);
smsBuilder.append(" ]\n\n");
} while (cursor.moveToNext());
} else {
smsBuilder.append("no result!");
}
smsBuilder.append("getSmsInPhone has executed!");
} catch (SQLiteException e) {
e.printStackTrace();
} finally {
cursor.close();
}
return smsBuilder.toString();
}
}
网友评论