作者:凌微茵_686 | 来源:互联网 | 2023-08-27 17:47
现在碰到这么一个需求,在已有App安装时,需要判断另一个app是否安装.以及自动安装.思路如下,在assets目录下.放另一个apk.文件.1.采取之前文章说到的,判断某个包名是否安装了.如果没有
现在碰到这么一个需求,在已有App安装时,需要判断另一个app是否安装.以及自动安装.
思路如下,在assets目录下.放另一个apk.文件.
1.采取之前文章说到的,判断某个包名是否安装了.
如果没有安装则跳转到第2步.
2.检测并安装插件制作.
package 你的包名;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.net.Uri;
import android.os.Environment;
import android.view.Gravity;
import android.widget.Toast;
public class GetApk extends CordovaPlugin {
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if (action.equals("GetApk")) {
Toast tips;
tips = Toast.makeText(cordova.getActivity(), "", Toast.LENGTH_SHORT);
tips.setGravity(Gravity.CENTER, 0, 0);
//这里的xxxx.apk我是放到assets目录下.
//也可以不要这段,直接用下面的下载
try {
if(copyApkFromAssets(cordova.getActivity(), "xxxx.apk", Environment.getExternalStorageDirectory().getAbsolutePath()+"/xxxx.apk"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath()+"/xxxx.apk"),
"application/vnd.android.package-archive");
cordova.getActivity().startActivity(intent);
}
else
{
new AlertDialog.Builder(cordova.getActivity())
.setTitle("install dhbm")
.setMessage("Please confirm you want to install dhbm")
.setPositiveButton("submit",
new OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
try {
Uri uri = Uri
.parse("http://www.xx.com/xxxx.apk");
Intent it = new Intent(
Intent.ACTION_VIEW,
uri);
cordova.getActivity().startActivity(it);
} catch (Exception e) {
}
}
})
.setNegativeButton("cancel",
new OnClickListener() {
public void onClick(
DialogInterface dialog,
int which) {
}
}).show();
}
}
catch (Exception e) {
e.printStackTrace();
}
return true;
}
return false;
}
public boolean copyApkFromAssets(Context context, String fileName, String path) {
boolean copyIsFinish = false;
try {
InputStream is = context.getAssets().open(fileName);
File file = new File(path);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
byte[] temp = new byte[1024];
int i = 0;
while ((i = is.read(temp)) > 0) {
fos.write(temp, 0, i);
}
fos.close();
is.close();
copyIsFinish = true;
} catch (IOException e) {
e.printStackTrace();
}
return copyIsFinish;
}
}
js 部分略过了.之前文章已有讲述.