Android 无法解析符号MY\u权限\u访问\u位置
我犯了和你一样的错误。所以我在活动课上创建了他的doCheckPermission。现在,有一个问题。活动课上说: java.lang.SecurityException:getCellLocation:用户10074和当前进程都没有android.permission.ACCESS\u位置 数据许可检查 我的舱单是:Android 无法解析符号MY\u权限\u访问\u位置,android,Android,我犯了和你一样的错误。所以我在活动课上创建了他的doCheckPermission。现在,有一个问题。活动课上说: java.lang.SecurityException:getCellLocation:用户10074和当前进程都没有android.permission.ACCESS\u位置 数据许可检查 我的舱单是: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.a
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.user.myapp>
<uses-feature
android:name="android.hardware.telephony"
android:required="true" />
<!-- Permission needed to read TelephoneManager data-->
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<!-- END -->
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".TelephonyStatusActivity"></activity>
</application>
</manifest>
我仍然会遇到与以前相同的错误您必须自己定义我的权限访问位置。这是一个您必须指定的字段。在使用它之前,您需要定义我的权限\u访问\u位置
public class MainActivity extends Activity{
int MY_PERMISSION_ACCESS_COARSE_LOCATION = 1;
private void doPermissionCheck() {
// This checks whether your app is granted permission to access coarse location of the user.
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
// if permission is not already granted to your app for location access then call requestPermissions to request that permission.
//This opens up a dialog before user to ask for permission.
//The integer MY_PERMISSION_ACCESS_COARSE_LOCATION that is passed acts like a unique identifier for this request and is used to
//track whether user has granted the permission or denied.
//See the below callback function onRequestPermissionsResult which is called after user had taken some action on that permission dialog
ActivityCompat.requestPermissions(TelephonyStatusActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSION_ACCESS_COARSE_LOCATION);
}
}
//This method is called after user has taken some action over the permission dialog
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
// Here you can access the result for your requested permission
// Now you use the integer MY_PERMISSION_ACCESS_COARSE_LOCATION which acts as a unique identifier for your permission
case MY_PERMISSION_ACCESS_COARSE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Permission is granted by the user on permission dialog
} else {
//Permission is not granted by the user on permission dialog
}
}
case : SOME_OTHER_PERMISSION_UNIQUE_ID : {
//This corresponds to some other permission request
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_telephony_status);
// you need to call the method here
doPermissionCheck();
final TelephonyManager telMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
btnPhoneStatus = (Button) findViewById(R.id.btnPhoneStatus);
btnEffacer = (Button) findViewById(R.id.btnEffacer);
txtPhoneStatus = (TextView) findViewById(R.id.txtPhoneStatus);
btnPhoneStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtPhoneStatus.setText(getTelephonyOverview(telMgr));
}
});
btnEffacer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtPhoneStatus.setText("");
}
});
}
}
自己定义权限
public static final int REQUEST_COARSE_LOCATION = 2;
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSION_ACCESS_COARSE_LOCATION );
没有与我的权限访问位置匹配的权限。您不能使用随机字符串并期望它工作。您是否定义了int MY_PERMISSION\u ACCESS\u rough\u LOCATION=;我在哪里定义它?看答案。您应该在类定义的下方,在和方法之前定义它。我应该为此提供什么int值?android.Manifest.permission.ACCESS\u rough\u LOCATION是字符串,如果我为我的\u permission\u ACCESS\u rough\u LOCATION解析它,它会解决我的问题吗?我得到了java.lang.NumberFormatException:输入字符串:android.permission.ACCESS\u rough\u LOCATION尝试输入我的\u permission\u rough\u LOCATION=Integer.parseIntManifest.permission.ACCESS\u位置;请参阅更新答案。整数是您的选择。它充当权限请求的唯一标识符。如果您请求多个权限,您可以使用唯一ID来标识它们,并知道哪些权限已被授予,哪些权限已被拒绝。onRequestPermissionsResult回调在用户通过权限对话框执行某些操作时触发。在此方法中,您可以检查每个请求的权限,并检查每个权限的结果。我在哪里定义它?
public class MainActivity extends Activity{
int MY_PERMISSION_ACCESS_COARSE_LOCATION = 1;
private void doPermissionCheck() {
// This checks whether your app is granted permission to access coarse location of the user.
if (ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED ) {
// if permission is not already granted to your app for location access then call requestPermissions to request that permission.
//This opens up a dialog before user to ask for permission.
//The integer MY_PERMISSION_ACCESS_COARSE_LOCATION that is passed acts like a unique identifier for this request and is used to
//track whether user has granted the permission or denied.
//See the below callback function onRequestPermissionsResult which is called after user had taken some action on that permission dialog
ActivityCompat.requestPermissions(TelephonyStatusActivity.this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSION_ACCESS_COARSE_LOCATION);
}
}
//This method is called after user has taken some action over the permission dialog
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
// Here you can access the result for your requested permission
// Now you use the integer MY_PERMISSION_ACCESS_COARSE_LOCATION which acts as a unique identifier for your permission
case MY_PERMISSION_ACCESS_COARSE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Permission is granted by the user on permission dialog
} else {
//Permission is not granted by the user on permission dialog
}
}
case : SOME_OTHER_PERMISSION_UNIQUE_ID : {
//This corresponds to some other permission request
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_telephony_status);
// you need to call the method here
doPermissionCheck();
final TelephonyManager telMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
btnPhoneStatus = (Button) findViewById(R.id.btnPhoneStatus);
btnEffacer = (Button) findViewById(R.id.btnEffacer);
txtPhoneStatus = (TextView) findViewById(R.id.txtPhoneStatus);
btnPhoneStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtPhoneStatus.setText(getTelephonyOverview(telMgr));
}
});
btnEffacer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
txtPhoneStatus.setText("");
}
});
}
}
public static final int REQUEST_COARSE_LOCATION = 2;
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSION_ACCESS_COARSE_LOCATION );