質問
私は知ることが可能であれば、そのプログラムによっ置を動的にダウンロードapkからカスタムAndroidアプリケーション.
解決
あなたは簡単にプレイストアのリンクを起動するか、プロンプトをインストールすることができます:
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("content:///path/to/your.apk"),
"application/vnd.android.package-archive");
startActivity(promptInstall);
または
Intent goToMarket = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.package.name"));
startActivity(goToMarket);
しかし、あなたは、ユーザーのの明示的な許可なしに.apksをインストールすることはできません。の。ないデバイスとプログラムが根ざしている場合を除きます。
他のヒント
File file = new File(dir, "App.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
私は同じ問題を抱えていたし、いくつかの試みの後に、それは私のためにこの方法を働きました。私はなぜ知っているが、データとタイプを設定すると、個別に私の意図を台無しにしないでください。
の解決にご提供いただいた質問すべてに適用 targetSdkVersion
s/23ます。Android N、APIレベルの24日は、しかししてもうまく動かないので、クラッシュ以下の例外:
android.os.FileUriExposedException: file:///storage/emulated/0/... exposed beyond app through Intent.getData()
これにより、安定したことからAndroid24 Uri
に対応し、ダウンロードしたファイルが変わりました。例えば、インストールという名前のファイル appName.apk
に保存された主な外部ファイルシステムのアプリパッケージ名 com.example.test
を実現しています。
file:///storage/emulated/0/Android/data/com.example.test/files/appName.apk
のための API 23
以下はよう
content://com.example.test.authorityStr/pathName/Android/data/com.example.test/files/appName.apk
のための API 24
以上です。
詳細はこ こちらの やせるわけにはいきませんじです。
との問いに答えるための targetSdkVersion
の 24
以上は、以下の手順を実行します。以下のエントリを追加するAndroidManifest.xml:
<application
android:allowBackup="true"
android:label="@string/app_name">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.authorityStr"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths"/>
</provider>
</application>
2.以下のように追加 paths.xml
ファイルの xml
フォルダ res
src-主要:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="pathName"
path="pathValue"/>
</paths>
の pathName
この模範的なコンテンツのuriの例では、上 pathValue
では実際のパスのシステム。こういうアイデアを入れて"."のあとに (引用)pathValue上記の場合を追加したくない余ったサブディレクトリの.
以下のようなコードを書くインストールapkの名前
appName.apk
の外部ファイルシステム:File directory = context.getExternalFilesDir(null); File file = new File(directory, fileName); Uri fileUri = Uri.fromFile(file); if (Build.VERSION.SDK_INT >= 24) { fileUri = FileProvider.getUriForFile(context, context.getPackageName(), file); } Intent intent = new Intent(Intent.ACTION_VIEW, fileUri); intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true); intent.setDataAndType(fileUri, "application/vnd.android" + ".package-archive"); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); context.startActivity(intent); activity.finish();
著者の許諾も必要な書くとき、自分のアプリのディレクトリの外部ファイルシステム.
まっているAutoUpdate図書館 こちらの を利用している。
まあ、私は深く掘って、AndroidのソースからPackageInstallerアプリケーションのソースを見つけます。
https://github.com/android/platform_packages_apps_packageinstallerする
マニフェストから私が見つけた、それは許可を必要とすること:
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
とインストールの実際のプロセスは、確認
の後に発生しますIntent newIntent = new Intent();
newIntent.putExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO, mPkgInfo.applicationInfo);
newIntent.setData(mPackageURI);
newIntent.setClass(this, InstallAppProgress.class);
String installerPackageName = getIntent().getStringExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME);
if (installerPackageName != null) {
newIntent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME, installerPackageName);
}
startActivity(newIntent);
私はちょうど私のapkファイルは私のアプリ「データ」ディレクトリに保存され、私は、それがそのようにインストールできるようにするために、世界読めるようにAPKファイルのアクセス権を変更するために必要なことされたという事実を共有したいですそれ以外のシステムは、「解析エラー:問題の解析があるパッケージ」を投げていました。そう@Horacemanからソリューションを使用して、その行います:
File file = new File(dir, "App.apk");
file.setReadable(true, false);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
startActivity(intent);
ハードコードに受信アプリを必要とし、それがため、より安全ではありませんしない別の解決策ます:
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData( Uri.fromFile(new File(pathToApk)) );
startActivity(intent);
はい、それは可能です。しかし、そのためにあなたは、未検証のソースをインストールするには、携帯電話を必要としています。例えば、slideMeはそれを行います。私はあなたができる最善のことは、アプリケーションが存在するかどうかを確認し、Androidマーケットのために意図を送信することだと思います。あなたは、Androidマーケットのために何かURLスキームを使用する必要があります。
market://details?id=package.name
私が活動を開始するために正確にどのように知りませんが、あなたは、URLのようなもので活動を開始した場合。これは、Androidマーケットを開き、ユーザーにアプリケーションをインストールするための選択肢を与える必要があります。
それの価値は、あなたのダウンロードをキックオフするDownloadManager
を使用する場合、例えば外部の場所に保存してくださいことを指摘setDestinationInExternalFilesDir(c, null, "<your name here>).apk";
。パッケージアーカイブタイプの意図は内部の場所にダウンロードして使用content:
スキームを好きなように見えるが、file:
のようにしていません。 (それはfile:
のURLになりにもかかわらずアプリがAPKを解析しませんように、Fileオブジェクトに内部パスをラップしようとすると、パスを取得することはどちらか動作しません。それのようなルックスは、外部でなければなりません。)
例:
int uriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
String downloadedPackageUriString = cursor.getString(uriIndex);
File mFile = new File(Uri.parse(downloadedPackageUriString).getPath());
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.fromFile(mFile), "application/vnd.android.package-archive")
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
appContext.startActivity(promptInstall);
ここでは他に見つかりませんでした。
初:
private static final String APP_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyAppFolderInStorage/";
private void install() {
File file = new File(APP_DIR + fileName);
if (file.exists()) {
Intent intent = new Intent(Intent.ACTION_VIEW);
String type = "application/vnd.android.package-archive";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri downloadedApk = FileProvider.getUriForFile(getContext(), "ir.greencode", file);
intent.setDataAndType(downloadedApk, type);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
intent.setDataAndType(Uri.fromFile(file), type);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
getContext().startActivity(intent);
} else {
Toast.makeText(getContext(), "ّFile not found!", Toast.LENGTH_SHORT).show();
}
}
第二: Android7以上定義する必要があるプロバイダのマニフェストのようです。
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="ir.greencode"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths" />
</provider>
三: 定義path.xml にres/xmlフォルダのように以下のとおりです。を使用しているこ パス 内部保存したい場合は変更するには何かが少しょう!できるこのリンク:FileProvider
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="your_folder_name" path="MyAppFolderInStorage/"/>
</paths>
項: を追加してくださいこの許可がマニフェスト:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
を確認してくださいプロバイダ当局は同じです!
これを試します
String filePath = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
String title = filePath.substring( filePath.lastIndexOf('/')+1, filePath.length() );
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(filePath)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
MainActivity.this.startActivity(intent);
UpdateNode の別のアプリケーション内からAPKパッケージをインストールするには、AndroidのためのAPIを提供します。
あなただけのオンラインアップデートを定義し、アプリにAPI統合することができます - それだけです。
現在、APIはベータ版の状態にあるが、あなたはすでにいくつかのテストを自分で行うことができます。
あなたはユーザーにとって重要な何かを伝えたい場合は、かなり便利 - 横にUpdateNodeの申し出にもかかわらず、システムメッセージが表示されています。
私は、クライアントの開発チームの一部だと私自身のAndroid Appの少なくともメッセージ機能を使用しています。
最初のAndroidManifest.xmlに以下の行を追加します:
<uses-permission android:name="android.permission.INSTALL_PACKAGES"
tools:ignore="ProtectedPermissions" />
次に、APKをインストールするには、次のコードを使用します:
File sdCard = Environment.getExternalStorageDirectory();
String fileStr = sdCard.getAbsolutePath() + "/MyApp";// + "app-release.apk";
File file = new File(fileStr, "TaghvimShamsi.apk");
Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
startActivity(promptInstall);
要求の権限に忘れないでください。
android.Manifest.permission.WRITE_EXTERNAL_STORAGE
android.Manifest.permission.READ_EXTERNAL_STORAGE
のAndroidManifest.xmlでプロバイダと権限を追加します:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
...
<application>
...
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
</application>
XMLファイルプロバイダを作成しますRES / XML / provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
のコード例以下の使用:
public class InstallManagerApk extends AppCompatActivity {
static final String NAME_APK_FILE = "some.apk";
public static final int REQUEST_INSTALL = 0;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// required permission:
// android.Manifest.permission.WRITE_EXTERNAL_STORAGE
// android.Manifest.permission.READ_EXTERNAL_STORAGE
installApk();
}
...
/**
* Install APK File
*/
private void installApk() {
try {
File filePath = Environment.getExternalStorageDirectory();// path to file apk
File file = new File(filePath, LoadManagerApkFile.NAME_APK_FILE);
Uri uri = getApkUri( file.getPath() ); // get Uri for each SDK Android
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData( uri );
intent.setFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK );
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
intent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME, getApplicationInfo().packageName);
if ( getPackageManager().queryIntentActivities(intent, 0 ) != null ) {// checked on start Activity
startActivityForResult(intent, REQUEST_INSTALL);
} else {
throw new Exception("don`t start Activity.");
}
} catch ( Exception e ) {
Log.i(TAG + ":InstallApk", "Failed installl APK file", e);
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG)
.show();
}
}
/**
* Returns a Uri pointing to the APK to install.
*/
private Uri getApkUri(String path) {
// Before N, a MODE_WORLD_READABLE file could be passed via the ACTION_INSTALL_PACKAGE
// Intent. Since N, MODE_WORLD_READABLE files are forbidden, and a FileProvider is
// recommended.
boolean useFileProvider = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N;
String tempFilename = "tmp.apk";
byte[] buffer = new byte[16384];
int fileMode = useFileProvider ? Context.MODE_PRIVATE : Context.MODE_WORLD_READABLE;
try (InputStream is = new FileInputStream(new File(path));
FileOutputStream fout = openFileOutput(tempFilename, fileMode)) {
int n;
while ((n = is.read(buffer)) >= 0) {
fout.write(buffer, 0, n);
}
} catch (IOException e) {
Log.i(TAG + ":getApkUri", "Failed to write temporary APK file", e);
}
if (useFileProvider) {
File toInstall = new File(this.getFilesDir(), tempFilename);
return FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, toInstall);
} else {
return Uri.fromFile(getFileStreamPath(tempFilename));
}
}
/**
* Listener event on installation APK file
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_INSTALL) {
if (resultCode == Activity.RESULT_OK) {
Toast.makeText(this,"Install succeeded!", Toast.LENGTH_SHORT).show();
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this,"Install canceled!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,"Install Failed!", Toast.LENGTH_SHORT).show();
}
}
}
...
}
これを試してみてください - マニフェストに書きます:
uses-permission android:name="android.permission.INSTALL_PACKAGES"
tools:ignore="ProtectedPermissions"
コードを書きます
File sdCard = Environment.getExternalStorageDirectory();
String fileStr = sdCard.getAbsolutePath() + "/Download";// + "app-release.apk";
File file = new File(fileStr, "app-release.apk");
Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(Uri.fromFile(file),
"application/vnd.android.package-archive");
startActivity(promptInstall);