2017-12-18 29 views
0

私は許可の使用を宣言している:Android OreoでPackageManager canRequestPackageInstallsを使用するには?私のアプリマニフェストに

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> 

をし、私のアプリは、未知のソースからインストールすることができれば私のコードでは、私がチェック:

public void reinstallApp(Activity activity, String pathname, int request_code) 
    { 
     if (activity.getPackageManager().canRequestPackageInstalls()) 
     { 
      try 
      { 
       Intent intent = new Intent(Intent.ACTION_VIEW); 
       intent.setDataAndType(Uri.fromFile(new File(pathname)), "application/vnd.android.package-archive"); 
       activity.startActivityForResult(intent, request_code); 
      } 
      catch (Exception e) 
      { 
       LogUtilities.show(this, e); 
      } 
     } 
     else 
     { 
      activity.startActivity(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", activity.getPackageName())))); 
     } 
    } 

けど「activity.getPackageManager ().canRequestPackageInstalls() "は、選択アクティビティでunknowソースからのallow installをチェックしても、常に" false "を返します。

問題点を教えてください。

答えて

1

最初に許可を求めてください。そのためには、不明な情報源からのインストール許可を得る必要があります。あなたのコードを並べ替えるだけで答えが得られました。

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 
      if (!getPackageManager().canRequestPackageInstalls()) { 
       startActivityForResult(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES).setData(Uri.parse(String.format("package:%s", getPackageName()))), 1234); 
      } else { 
       callInstallProcess(); 
      } 
     } else { 
      callInstallProcess(); 
     } 

上記のコードはあなたのonCreate()にあります。結果を確認することができます。

@RequiresApi(api = Build.VERSION_CODES.O) 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == 1234 && resultCode == Activity.RESULT_OK) { 
     if (getPackageManager().canRequestPackageInstalls()) { 
      callInstallProcess(); 
     } 
    } else { 
     //give the error 
    } 
} 

ここで、インストールはcallInstallProcess()で行われています。

 try 
     { 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setDataAndType(Uri.fromFile(new File(pathname)), "application/vnd.android.package-archive"); 
      activity.startActivityForResult(intent, request_code); 
     } 
     catch (Exception e) 
     { 
      LogUtilities.show(this, e); 
     } 

はAndroidManifest.xmlを

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" /> 
で許可を与えることを忘れないでください。
関連する問題