반응형

기기가 Android 5.1 API Level 22(Lollipop) 이하를 실행 하거나 앱의 targetSdkVersion이 22 이하인 경우, 아래와 같이 AndroidManifest.xml 파일에만 SEND_SMS(위험 권한)를 요청하면 문자를 보낼 수 있다.


1
<uses-permission android:name="android.permission.SEND_SMS"/>


그러므로 아래와 같이 간단히 문자를 보내는 코드는 API Level 23(Marshmallow)이전, API Level 22(Lollipop)까지만 가능 하다.


<AndroidManifest.xml>

1
<uses-permission android:name="android.permission.SEND_SMS"/>



<MainActivity.java>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class MainActivity extends AppCompatActivity {
 
    EditText editText1;
    EditText editText2;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        editText1 = findViewById(R.id.phoneNumber);
        editText2 = findViewById(R.id.message);
 
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phoneNumber = editText1.getText().toString();
                String message = editText2.getText().toString();
 
                try {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(phoneNumber, null, message, nullnull);
                    Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });
    }
}



기기가 Android 6.0 API Level 23(Marshmallow) 이상을 실행 하거나 앱의 targetSdkVersion이 23 이상인 경우 앱은 런타임에 사용자로부터 권한을 요청해야 한다. 사용자가 언제든지 권한을 취소할 수 있으므로, 앱이 실행할 때마다 권한을 갖고 있는지 여부를 확인해야 한다.


<AndroidManifest.xml>

1
<uses-permission android:name="android.permission.SEND_SMS"/>



<MainActivity.java>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
public class MainActivity extends AppCompatActivity {
 
    private final int MY_PERMISSION_REQUEST_SMS = 1001;
 
    EditText editText1;
    EditText editText2;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS) != PackageManager.PERMISSION_GRANTED) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SEND_SMS)) {
                // Android provides a utility method, shouldShowRequestPermissionRationale(), that returns true if the user has previously
                // denied the request, and returns false if a user has denied a permission and selected the Don't ask again option in the
                // permission request dialog, or if a device policy prohibits the permission. If a user keeps trying to use functionality that
                // requires a permission, but keeps denying the permission request, that probably means the user doesn't understand why
                // the app needs the permission to provide that functionality. In a situation like that, it's probably a good idea to show an
                // explanation.
 
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("info");
                builder.setMessage("This app won't work properly unless you grant SMS permission.");
                builder.setIcon(android.R.drawable.ic_dialog_info);
 
                builder.setNeutralButton("OK"new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ActivityCompat.requestPermissions(MainActivity.thisnew String[] {Manifest.permission.SEND_SMS}, MY_PERMISSION_REQUEST_SMS);
                    }
                });
 
                AlertDialog dialog = builder.create();
                dialog.show();
            } else {
                ActivityCompat.requestPermissions(thisnew String[] {Manifest.permission.SEND_SMS}, MY_PERMISSION_REQUEST_SMS);
            }
        }
 
        editText1 = findViewById(R.id.phoneNumber);
        editText2 = findViewById(R.id.message);
 
        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phoneNumber = editText1.getText().toString();
                String message = editText2.getText().toString();
 
                try {
                    SmsManager smsManager = SmsManager.getDefault();
                    smsManager.sendTextMessage(phoneNumber, null, message, nullnull);
                    Toast.makeText(getApplicationContext(), "Message sent", Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    e.printStackTrace();
                }
            }
        });
    }
 
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSION_REQUEST_SMS: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0 && grantResults[0== PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(this,"Permission granted.",Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this,"Permission denied.",Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}




 


2018년 11월 1일 이후로 구글 플레이에서는 위 예제와 같이 SMS 및 Call Log 권한을 요청하는 앱을 삭제하고 등록을 막기 시작했다.

Reminder SMS/Call Log Policy Changes


※참고

Permissions overview


반응형
Posted by J-sean
: