我正在尝试使用getPermissiontoReadSMS
类中的onRequestPermissionsResult
和permissionsSMSHelper
方法。当我在ewMessagePage.java
文件和onCreate
文件的homePage.java
方法中引用它们时,它会给我"无法解析方法"每次都有错误。我无法弄清楚出了什么问题。如果有人可以提供帮助,我会很感激。
permisssionsSMSHelper.java
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.widget.Toast;
public class permissionsSMSHelper extends homePage {
public int READ_SMS_PERMISSIONS_REQUEST;
public permissionsSMSHelper(int req) {
READ_SMS_PERMISSIONS_REQUEST = req;
}
public void getPermissionToReadSMS(int reqVal) {
READ_SMS_PERMISSIONS_REQUEST = reqVal;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (shouldShowRequestPermissionRationale(
Manifest.permission.READ_SMS)) {
Toast.makeText(this, "Please allow permission", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]{Manifest.permission.READ_SMS}, reqVal);
}
}
public RequestPermissionsResult(int req) {
READ_SMS_PERMISSIONS_REQUEST = req;
}
public void onRequestPermissionsResult(int requestCode,
int requestNum,
@NonNull String permissions[],
@NonNull int[] grantResults) {
READ_SMS_PERMISSIONS_REQUEST= requestNum;
if(requestCode == requestNum) {
if(grantResults.length == 1 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Read SMS permission granted", Toast.LENGTH_SHORT).show();
refreshSmsInbox();
}else {
Toast.makeText(this, "Read SMS permission denied", Toast.LENGTH_SHORT).show();
}
}else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
homePage.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
mDrawerList = (ListView)findViewById(R.id.navList);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
messages = (ListView) findViewById(R.id.messages);
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, smsMessagesList);
messages.setAdapter(arrayAdapter);
if(ContextCompat.checkSelfPermission(this,Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED) {
permissionsSMSHelper SMScheck = new permissionsSMSHelper(1);
SMScheck.getPermissionToReadSMS(1);
SMScheck.onRequestPermissionsResult(1);
}else {
refreshSmsInbox();
}
addDrawerItems();
FABListener();
}
newMessagePage.java
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class newMessagePage extends homePage {
SmsManager smsManager = SmsManager.getDefault();
EditText input;
public void onSendClick(View view) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
permissionsSMSHelper SMScheck = new permissionsSMSHelper(1);
SMScheck.getPermissionToReadSMS(1);
SMScheck.onRequestPermissionsResult(1);
}else {
smsManager.sendTextMessage("17739965441", null, input.getText().toString(), null, null);
Toast.makeText(this, "Message sent!", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_message_page);
input = (EditText) findViewById(R.id.input);
}
}
答案 0 :(得分:0)
你做错了。您所谓的PermissionsSMSHelper
课程正在扩展HomePage
,您正在permissionsSMSHelper
中创建HomePage
的实例,而NewMessagePage
的情况也是extends homePage
引用另一个类中的活动,这将导致内存泄漏。从PermissionsSMSHelper
中删除Context
并在构造函数中传递{{1}}或在PermissonsSMSHelper中使方法静态,并在每个方法调用中传递Context。 您的命名惯例太糟糕了