我正在创建一个允许自动添加Android 6.0和Android M权限的应用程序。我的权限包括
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
我使用下面的代码添加Android 6.0版本的权限
public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(checkAndRequestPermissions()) {
Log.d ("TAG", "Permissions are granted");
}
}
private boolean checkAndRequestPermissions() {
if (Build.VERSION.SDK_INT >= 23) {
int permissionSTORAGE = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
int permissionRECORDAUDIO = ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO);
List<String> listPermissionsNeeded = new ArrayList<>();
if (permissionSTORAGE != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (permissionRECORDAUDIO != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.RECORD_AUDIO);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}
else
return true;
}}
我在上面的代码中有两个问题。
答案 0 :(得分:2)
请正确阅读我的代码并根据需要进行修改
只需在 public MemberEntity Search(string prefix)
{
try
{
MemberEntity ObjMember = new MemberEntity();
string sql = string.Format(@"select first_name, last_name from member_master where first_name like ('@prefix')");
using (MySqlConnection conn = new MySqlConnection(UtilityHelper.getConn()))
{
//using (MySqlCommand cmd = new MySqlCommand(string.Format("select first_name, last_name from member_master where first_name like ('@prefix%')"), conn))
using (MySqlCommand cmd = new MySqlCommand(sql.ToString(), conn))
{
conn.Open();
cmd.Parameters.AddWithValue("@prefix", prefix);
cmd.CommandType = CommandType.Text;
using (MySqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
ObjMember.Name = string.Format("{0} {1}", dr["first_name"].ToString(), dr["last_name"].ToString());
}
}
}
}
return ObjMember;
}
catch (Exception ex)
{
throw ex;
}
#endregion
}
来电中包含所有4个权限,Android就会像您提到的那样自动将它们一起分页。
我有一个帮助方法来检查多个权限,看看是否有任何权限被授予。
ActivityCompat.requestPermissions(...)
然后只发送所有权限。 Android只会询问它需要的那些。
public static boolean hasPermissions(Context context, String... permissions) {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
答案 1 :(得分:1)