package com.akilli.sms;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView lvSMS;
private Button btnRefresh;
private final static int REQUEST_CODE_PERMISSION_READ_SMS=456;
ArrayList<String> smsMsgList = new ArrayList<String>();
ArrayAdapter arrayAdapter;
public static MainActivity instance;
public static MainActivity Instance() {
return instance;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRefresh=findViewById(R.id.btn_refresh);
instance=this;
lvSMS=(ListView) findViewById(R.id.lv_sms);
arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,smsMsgList);
lvSMS.setAdapter(arrayAdapter);
if (checkPermission(Manifest.permission.READ_SMS)) {
refreshInbox();
}else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{
(Manifest.permission.READ_SMS)}, REQUEST_CODE_PERMISSION_READ_SMS );
}
}
private boolean checkPermission(String readSms) {
return true;
}
public void refreshInbox(){
ContentResolver cResolver= getContentResolver();
Cursor smsInboxCursor =cResolver.query(Uri.parse("content://sms/inbox"),null,null,null,null);
int indexBody = smsInboxCursor.getColumnIndex("body");
int indexAddress = smsInboxCursor.getColumnIndex("address");
if (indexBody <0 || !smsInboxCursor.moveToFirst()) return;
arrayAdapter.clear();
do {
String str= "SMS from : "+ smsInboxCursor.getString(indexAddress)+"\n";
str += smsInboxCursor.getString(indexBody);
arrayAdapter.add(str);
} while (smsInboxCursor.moveToNext());
}
public void updateList(final String smsMsg) {
arrayAdapter.insert(smsMsg, 0);
arrayAdapter.notifyDataSetChanged();
}
public void refreshInbox(View view) {
}
}
Java Kodu bu şekilde
package com.akilli.sms;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView lvSMS;
private Button btnRefresh;
private final static int REQUEST_CODE_PERMISSION_READ_SMS=456;
ArrayList<String> smsMsgList = new ArrayList<String>();
ArrayAdapter arrayAdapter;
public static MainActivity instance;
public static MainActivity Instance() {
return instance;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRefresh=findViewById(R.id.btn_refresh);
instance=this;
lvSMS=(ListView) findViewById(R.id.lv_sms);
arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,smsMsgList);
lvSMS.setAdapter(arrayAdapter);
if (checkPermission(Manifest.permission.READ_SMS)) {
refreshInbox();
}else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{
(Manifest.permission.READ_SMS)}, REQUEST_CODE_PERMISSION_READ_SMS );
}
}
private boolean checkPermission(String readSms) {
return true;
}
public void refreshInbox(){
ContentResolver cResolver= getContentResolver();
Cursor smsInboxCursor =cResolver.query(Uri.parse("content://sms/inbox")
,null,null,null,null);
int indexBody = smsInboxCursor.getColumnIndex("body");
int indexAddress = smsInboxCursor.getColumnIndex("address");
if (indexBody <0 || !smsInboxCursor.moveToFirst()) return;
arrayAdapter.clear();
do {
String str= "SMS from : "+ smsInboxCursor.getString(indexAddress)+"\n";
str += smsInboxCursor.getString(indexBody);
arrayAdapter.add(str);
} while (smsInboxCursor.moveToNext());
}
public void updateList(final String smsMsg) {
arrayAdapter.insert(smsMsg