我正在编写一个通过TCP Socket发送消息的应用程序。我正在努力获得互联网许可。 (API 26是目标)
socket EACCESS permission denied
我已在我的Manfest中设置:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.seb.sebastien.opengardenirc">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.persmission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
当我使用API26时,我必须在代码中添加权限检查以确保它正常工作:
if (shouldAskPermission()) {
Log.d(TAG, "Permission is required");
String[] PERMISSIONS_STORAGE = {
Manifest.permission.ACCESS_NETWORK_STATE,
Manifest.permission.INTERNET,
Manifest.permission.ACCESS_WIFI_STATE
};
int permission_internet = ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET);
int permission_network = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_NETWORK_STATE);
int permission_wifi = ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_WIFI_STATE);
Log.d(TAG, "Package Manager: " + PackageManager.PERMISSION_GRANTED);
Log.d(TAG, "INTERNET : " + permission_internet);
Log.d(TAG, "NETWORK : " + permission_network);
Log.d(TAG, "WIFI : " + permission_wifi);
if ((permission_internet != PackageManager.PERMISSION_GRANTED)
|| (permission_network != PackageManager.PERMISSION_GRANTED)
|| (permission_wifi != PackageManager.PERMISSION_GRANTED)){
Log.d(TAG, "Asking for permission");
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this,
PERMISSIONS_STORAGE,
REQUEST_CONNECTION_PERMISSION
);
} else {
Toast.makeText(MainActivity.this, "Permission (already) Granted!", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Permission already granted");
startTcpInAThread("str");
}
}
我还有抓住onRequestPermissionsResult
的方法。
private boolean shouldAskPermission(){
return(Build.VERSION.SDK_INT> Build.VERSION_CODES.LOLLIPOP_MR1);
}
@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){
switch(permsRequestCode){
case REQUEST_CONNECTION_PERMISSION:
Log.d(TAG, "Permission granted result : " + grantResults[0]);
Log.d(TAG, "Permission granted result : " + grantResults[1]);
Log.d(TAG, "Permission granted result : " + grantResults[2]);
boolean accepted = (grantResults[0]== PackageManager.PERMISSION_GRANTED)
&& (grantResults[1]== PackageManager.PERMISSION_GRANTED)
&& (grantResults[2]== PackageManager.PERMISSION_GRANTED);
if(accepted) {
startTcpInAThread("str");
} else {
Toast.makeText(getApplicationContext(), "Permissions issues!!!!", Toast.LENGTH_SHORT).show();
}
break;
}
}
首先,在检查权限时,我可以看到android.persmission.INTERNET
未被授予。好的是,它会调用请求权限方法ActivityCompat.requestPermissions
我期待一个对话框出现,但我的理解是dlgbox只显示一些其他人不需要的权限请求。
我担心的是,当我获得onRequestPermissionsResult
时,仍未获得许可。
有人知道如何授予互联网许可吗?
我还注意不要在同一个线程中运行网络访问。
此致