Soru & Cevap

android studio READ_SMS izin isteği

19.05.2020 - 05:32

Merhaba , Android studio da gelen mesajları gösteren bir uygulama yaptım.

ama uygulamayı açınca izin istemiyor .

"private boolean checkPermission(String readSms) { return true; } 

bu kısım return true olunca uygulama izin istemiyor. return false olunca izin ekranı geliyor

ama program mesaj almıyor. bu sorunu nasıl giderebilirim şimdiden teşşkür ederim.

kodu yazınca hatalar aldığım için resim koydum.

5 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

picture-276029-1590247737.jpg
gxslash
20.05.2020 - 07:28

clear() metodunu arrayAdapter yerine smsMsgList üzerinde uygulayınız. Ve mesajları yine arrayAdapter yerine smsMsgList'e ekleyiniz.

package com.gxslash.smsreader;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

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.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

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<>();
    ArrayAdapter arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnRefresh = findViewById(R.id.btn_refresh);
        lvSMS = findViewById(R.id.lv_sms);

        checkPermission();
    }

    private void checkPermission() {

        String[] permissions = new String[]{Manifest.permission.READ_SMS};
        ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE_PERMISSION_READ_SMS);
    }

    public List<String> getAllSms() {

        List<String> lstSms = new ArrayList<>();

        Cursor c = getContentResolver().query(Uri.parse("content://sms/"), null, null, null, null);
        int totalSMS = c.getCount();

        if (c.moveToFirst()) {
            for (int i = 0; i < totalSMS; i++) {

                StringBuffer buffer = new StringBuffer();

                String id = c.getString(c.getColumnIndexOrThrow("_id"));
                String address = c.getString(c
                        .getColumnIndexOrThrow("address"));
                String body = c.getString(c.getColumnIndexOrThrow("body"));
                String read = c.getString(c.getColumnIndex("read"));
                String date = c.getString(c.getColumnIndexOrThrow("date"));

                String folderName = "";
                if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) {
                    folderName = "inbox";
                } else {
                    folderName = "sent";
                }

                buffer.append(id + "&");
                buffer.append(address + "&");
                buffer.append(body + "&");
                buffer.append(read + "&");
                buffer.append(date + "&");
                buffer.append(folderName + "&");

                lstSms.add(buffer.toString());
                c.moveToNext();
            }
        }
        return lstSms;
    }


    public void refreshInbox(View view) {
        if (view.getId() == R.id.btn_refresh) {
            listSms();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == REQUEST_CODE_PERMISSION_READ_SMS) {

            String permission = permissions[0];
            int grantResult = grantResults[0];

            if (permission.equals(Manifest.permission.READ_SMS)) {
                if (grantResult == PackageManager.PERMISSION_GRANTED) {
                    listSms();
                } else {
                    requestPermissions(new String[]{Manifest.permission.READ_SMS}, REQUEST_CODE_PERMISSION_READ_SMS);
                }
            }

        }
    }

    private void listSms() {
        smsMsgList.clear();
        smsMsgList.addAll(getAllSms());
        arrayAdapter = new A
picture-276029-1590247737.jpg
gxslash
20.05.2020 - 07:27

clear() metodunu arrayAdapter yerine smsMsgList üzerinde uygulayınız. Ve mesajları yine arrayAdapter yerine smsMsgList'e ekleyiniz.

package com.gxslash.smsreader;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

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.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

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<>();
    ArrayAdapter arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnRefresh = findViewById(R.id.btn_refresh);
        lvSMS = findViewById(R.id.lv_sms);

        checkPermission();
    }

    private void checkPermission() {

        String[] permissions = new String[]{Manifest.permission.READ_SMS};
        ActivityCompat.requestPermissions(this, permissions, REQUEST_CODE_PERMISSION_READ_SMS);
    }

    public List<String> getAllSms() {

        List<String> lstSms = new ArrayList<>();

        Cursor c = getContentResolver().query(Uri.parse("content://sms/"), null, null, null, null);
        int totalSMS = c.getCount();

        if (c.moveToFirst()) {
            for (int i = 0; i < totalSMS; i++) {

                StringBuffer buffer = new StringBuffer();

                String id = c.getString(c.getColumnIndexOrThrow("_id"));
                String address = c.getString(c
                        .getColumnIndexOrThrow("address"));
                String body = c.getString(c.getColumnIndexOrThrow("body"));
                String read = c.getString(c.getColumnIndex("read"));
                String date = c.getString(c.getColumnIndexOrThrow("date"));

                String folderName = "";
                if (c.getString(c.getColumnIndexOrThrow("type")).contains("1")) {
                    folderName = "inbox";
                } else {
                    folderName = "sent";
                }

                buffer.append(id + "&");
                buffer.append(address + "&");
                buffer.append(body + "&");
                buffer.append(read + "&");
                buffer.append(date + "&");
                buffer.append(folderName + "&");

                lstSms.add(buffer.toString());
                c.moveToNext();
            }
        }
        return lstSms;
    }


    public void refreshInbox(View view) {
        if (view.getId() == R.id.btn_refresh) {
            listSms();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == REQUEST_CODE_PERMISSION_READ_SMS) {

            String permission = permissions[0];
            int grantResult = grantResults[0];

            if (permission.equals(Manifest.permission.READ_SMS)) {
                if (grantResult == PackageManager.PERMISSION_GRANTED) {
                    listSms();
                } else {
                    requestPermissions(new String[]{Manifest.permission.READ_SMS}, REQUEST_CODE_PERMISSION_READ_SMS);
                }
            }

        }
    }

    private void listSms() {
        smsMsgList.clear();
        smsMsgList.addAll(getAllSms());
        arrayA
Hasan aslan
20.05.2020 - 06:45
merhaba tam anlayamadım k. bakmayın android sudio da yeniyim. tam olarak ne yapmam gerekiyor
Profile picture for user aslanhasan
aslanhasan
19.05.2020 - 05:36
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.
Profile picture for user aslanhasan
aslanhasan
19.05.2020 - 05:35
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
Profile picture for user aslanhasan
aslanhasan
19.05.2020 - 05:34

 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();