웹서버에서 apk를 올려놓고 특정 txt 파일을 읽어 버전이 다르면 다운로드하라는 메세지를 띄우고 apk 를 다운로드 후 실행하는 코드를 만들었습니다.
그러나 '패키지 파일을 분할하는 중에 문제가 발생하였습니다.' 라는 메세지가 출력이 되는데 어느 부분을 손봐야할지 감이 잘 오지 않습니다.
도움 부탁드립니다.
Handler handler1 = new Handler(){
public void handleMessage(Message msg){
if(msg.obj.toString().equals("ok")) {
AlertDialog alert = new AlertDialog.Builder( MainActivity.this )
.setTitle( "Notice" )
.setMessage( "Find new version. You must update apk" )
.setPositiveButton( "confirm", new DialogInterface.OnClickListener(){
@SuppressLint("NewApi")
@Override
public void onClick(DialogInterface dialog, int which) {
int idx=strUpdateApk.lastIndexOf("/");
String downApk=strUpdateApk.substring(idx+1);
String path= Environment.getDataDirectory().getAbsolutePath();
path+="/data/com.mcc.mcc_smart_medi_jeju/files"+downApk;
if(new File(path).exists()){
Toast.makeText(getApplicationContext(), There is a already file".",0).show();
}else{
(new DownThread(strUpdateApk,downApk)).start();
} }
}).show();
} else {
handler1.removeMessages(0);
}
}
};
class DownThread extends Thread{
String mAddr;
String mFile;
DownThread(String addr, String filename){
mAddr= addr;
mFile = filename;
}
@Override
public void run() {
URL apkurl;
int Read;
try{
apkurl= new URL(mAddr);
HttpURLConnection conn = (HttpURLConnection)apkurl.openConnection();
int len= conn.getContentLength();
byte[] raster= new byte[len];
InputStream is = conn.getInputStream();
FileOutputStream fos = openFileOutput(mFile,0);
for(;;){
Read=is.read(raster);
if(Read<=0){
break;
}
fos.write(raster,0,Read);
}
is.close();
fos.close();
conn.disconnect();
}catch(Exception e){
mFile=null;
}
Message message=mAfterDown.obtainMessage();
message.obj=mFile;
mAfterDown.sendMessage(message);
}
}
Handler mAfterDown=new Handler(){
public void handleMessage(Message msg){
if(msg.obj!=null){
String path=Environment.getDataDirectory().getAbsolutePath();
path+="/data/com.mcc.mcc_smart_medi_jeju/files"+(String)msg.obj;
Intent intent= new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
startActivity(intent);
}
}
};