gpt4 book ai didi

java - 从BroadcastReciever内部的startActivityForResult获取结果,调用ACTION_INSTALL_PACKAGE Intent

转载 作者:行者123 更新时间:2023-12-02 01:43:22 26 4
gpt4 key购买 nike

我正在创建一个应用程序,用户可以从中下载其他应用程序并安装它们。现在它工作正常,但安装后它不会删除apk。我尝试过使用 BroadcastRecievers 但他们似乎不明白应用程序何时已安装。

目前我正在尝试startActivityForResult,完成后,从文件中删除apk

public class Updater {
private static BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {

@Override
public void onReceive(Context context, Intent intent) {
Logger.d("Download completed.");
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath(), downloadApp.getDownloadKey());
Logger.i("Opening: " + file.getAbsolutePath());

Intent openDownloadIntent = getOpenDownloadedApkIntent(context, file);

RelativeLayout progressView = progressViewReference.get();
if (progressView!= null) {
progressView.setVisibility(View.GONE);
}

try {
((Activity) context).startActivityForResult(openDownloadIntent, getResultCode());


} catch (ActivityNotFoundException e) {
// TODO: more robust error handling (show dialog or something)
Logger.e("Exception when launching download intent, message:" + e.getMessage());
}
}
};

private static Intent getOpenDownloadedApkIntent(Context context, File file) {

// The type of intent to use to open the downloaded apk changed in Android N (7.0)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri path = FileProvider.getUriForFile(context,
context.getApplicationContext().getPackageName() + ".utils.DownloadedFileProvider",
file);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.putExtra(Intent.EXTRA_RETURN_RESULT, true);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setData(path);
return intent;
} else {
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
return intent;
}
}
}

所以我有 Updater 类,其中有两个方法(请参阅上面的代码)。并且此类正在由适配器调用,因此没有地方放置 onActivityResult。我尝试将其放在调用 AdapterActivity 中,后者调用 Updater 类,但即使使用 也无法到达那里EXTRA_RETURN_RESULT

所以我的问题是..安装完成后如何在这里调用onActivityResult

最佳答案

这是您 Activity 的伪代码

class DownloadActivity extends AppCompatActivity {
// some of your code here

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
// in here you are checking if requestCode is equal to Intent.ACTION_INSTALL_PACKAGE
if(requestCode == Intent.ACTION_INSTALL_PACKAGE and resultCode == ResultCode.OK) {
// Start activity for deleting file
startActivityForResult(Intente.ACTION_DELETE);
}

if(requestCode == Intent.ACTION_DELETE and resultCode == ResultCode.OK) {
// Handling actions after deleted
}
}
}

当操作结束时,OnActivityResult 由 android 调用。您不必仅在您定义必须执行的操作的方法内调用此方法。

那些 onDownloadComplete 和 getOpenDownloadedApkIntent 是在哪里定义和调用的?

关于java - 从BroadcastReciever内部的startActivityForResult获取结果,调用ACTION_INSTALL_PACKAGE Intent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57476191/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com