请求权限

时间:2018-05-13 18:36:06

标签: android

我正在开发一款允许用户在服务器上传文件的应用。 我需要请求权限,但应用程序没有显示询问权限的对话框。 PS。我在清单中添加了权限。

Java代码

public class MainActivity extends AppCompatActivity {

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); //requestPermissions(new String[]{Manifest.permission.INTERNET}, 1); ActivityCompat.requestPermissions(MainActivity.this,new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, 1); ActivityCompat.requestPermissions(MainActivity.this,new String[] {Manifest.permission.INTERNET}, 1); } } public void Upload() { TextView Result= (TextView) findViewById(R.id.Conn); String URL= ((EditText) findViewById(R.id.URL)).getText().toString(); String Username= ((EditText) findViewById(R.id.Username)).getText().toString(); String Password= ((EditText) findViewById(R.id.Password)).getText().toString(); FTPClient FTPClient=new FTPClient(); FTPClient.enterLocalPassiveMode(); try { InetAddress indirizzo = InetAddress.getByName(URL); FTPClient.connect(indirizzo); Result.setText(FTPClient.getReplyString()); //aperta la connessione al server procedo al login boolean login=FTPClient.login(Username,Password); if(login==true) Result.append(" ..login effettuato"); else Result.append("...login fallito"); //apro il file contenuto nel path /sdcard come stream /*client.setFileType(FTP.BINARY_FILE_TYPE); // imposto il tipo di dati, da modificare a seconda dell'utilità File sdcardDir = Environment.getExternalStorageDirectory(); //dato che il mio file era sulla sd prendo il riferimento alla memoria esterna File file = new File(sdcardDir,"fotografia.jpeg"); //prendo il riferimento al file salvato sulla sd FileInputStream fis = new FileInputStream(file);//creo uno stream in input a partire dal mio file //risposta.append("...path file: " + file.getPath()); //questa istruzione la usavo per test, la lascio casomai dovesse servire, stampa semplicemente il path del file che voglio uploadare boolean upload_ok=client.storeFile("fotografia.jpeg", fis); fis.close();//procedo alla chiusura dello stream, infatti storeFile non chiude fis if(upload_ok==true)risposta.append(" ..... upload ok"); else risposta.append("....upload fallito, errore: " + upload_ok);*/ } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case 1: { // If request is cancelled, the result arrays are empty. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { // permission granted Toast.makeText(MainActivity.this, "Permission denied", Toast.LENGTH_SHORT).show();} else { // permission denied Toast.makeText(MainActivity.this, "Permission denied", Toast.LENGTH_SHORT).show(); } return; } } }

抱歉我给你造成的每一个麻烦和我的英语不好,也许这很简单,但我还在学习

1 个答案:

答案 0 :(得分:0)

您应该一次查询两个权限:

ActivityCompat.requestPermissions(MainActivity.this,new String[]
            {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.INTERNET}, 1);

然后在回调中为请求的权限字符串添加开关。

您可能还想添加shouldShowRequestPermissionRationale()。如果用户选中了“请不要再问”#34;在请求期间,对话框不再显示。