0

I have create program which can send message. when I use Activity.checkSelfPermission, it show the error like "The method checkSelfPermission(MainActivity, String) is undefined for the type ActivityCompat". I have import android.support.v4.app.ActivityCompat already. My target API 23 and compile with API 23 also. How to solve it?

enter image description here

enter image description here

Below is the real code

public class MainActivity extends Activity {

public static final int MY_PERMISSION_SEND_SMS = 10;
public EditText edSMS, edPhone;
public Button btnSent;

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

    edSMS = (EditText)findViewById(R.id.editText1);
    edPhone = (EditText)findViewById(R.id.editText2);
    btnSent = (Button)findViewById(R.id.button1);
    onCheckPermission();
}

private void onCheckPermission() {
    if(ActivityCompat.checkSelfPermission(this,Manifest.permission.SEND_SMS)!= PackageManager.PERMISSION_GRANTED){
        ActivityCompat.requestPermission(this, new String[]{Manifest.permission.SEND_SMS}, MY_PERMISSION_SEND_SMS);
    }       
    else {
        sentMessage();
    }
}
private void sentMessage() {
    btnSent.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String phoneNumber = edPhone.getText().toString();
            String SMS = edSMS.getText().toString();
            if(((phoneNumber.length() == 10) || (phoneNumber.length()==9)) && phoneNumber.length()>0){
                SmsManager smsText = SmsManager.getDefault();
                smsText.sendTextMessage(phoneNumber, null, SMS, null, null);
                Toast.makeText(MainActivity.this, "SMS was sent successful", Toast.LENGTH_LONG).show();
                edPhone.setText("");
                edSMS.setText("");
            } else {
                Toast.makeText(MainActivity.this, "Please check your " + "phone number again",Toast.LENGTH_LONG).show();
            }
        }
    });
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults){
    switch (requestCode){
        case MY_PERMISSION_SEND_SMS:
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Toast.makeText(this, "Read Contacts permission granted", Toast.LENGTH_SHORT).show();
                sentMessage();
            }else{
                Toast.makeText(this, "Read Contacts permission denied", Toast.LENGTH_SHORT).show();
                if(ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SEND_SMS)){
                    new AlertDialog.Builder(this).
                    setTitle("Request Permission SMS").
                    setMessage(" You must set permission to access this application").show();
                }
            }
            break;
    }
}
Phearum Chheang
  • 89
  • 1
  • 2
  • 11

2 Answers2

0

To solve the method ActivityCompat.checkSelfPermission() not found in Eclipse you can simply add android-support-compat.jar as taken from https://github.com/dandar3/android-support-compat/tree/28.0.0 to the libs folder of your Eclipse project and compile again.

mougino
  • 33
  • 4
-1

just use support.v7 (import android.support.v7.app.AppCompatActivity;),this is well be rolved

by the way?why do you still use eclipse now?just use Android studio,it's better so much.

Mr.jie
  • 42
  • 6