您的位置:首页 > 移动开发 > Android开发

Android 没有SD卡,利用手机存储下载、自动安装,解决Parse error when parsing manifest问题

2015-09-13 17:52 597 查看
做接受老大的要求,一定要在今天完成下载安装apk的方法;

下载安装在有SD卡的手机、电视上是很容易的实现的,网上有方法众多;

但是没有SD卡的情况下却不是那么容易找,我找了很久,也没有人给个具体的答案,最后自己解决的方案如下:

1、解析包错误,重点在权限上,无论你下载到手机的哪,都要权限;如果没有会有如下的提示错误:

<spanstyle="font-size:18px;">Parseerrorwhenparsingmanifest.Discontinuinginstallation
AndroidManifest.xmlof/data/data/com.hsee.o2o/files/***meitu.apk
java.io.FileNotFoundException:AndroidManifest.xml</span>

可以看出是权限的问题,就是不能对文件操作,那就对apk修改权限不就行了。

修改权限的方法如下:

/**
	*@paramupdateDir就是可以执行的文件
	*void修改文件的权限,可读、可写、可执行
	*@date2015年9月13日
	*@authorliuyonghong
	*/
	privatevoidsetUpdateDir(FileupdateDir){
		try{
			Processp=Runtime.getRuntime().exec("chmod777"+updateDir);
			intstatus=p.waitFor();
		}catch(IOExceptione){
			//TODOAuto-generatedcatchblock
			e.printStackTrace();
		}catch(InterruptedExceptione){
			//TODOAuto-generatedcatchblock
			e.printStackTrace();
		}
		
	}

2、确定文件路径和文件名字

<preclass="html"name="code">//这个判断是有SD卡的情况的下载情况路径
if(android.os.Environment.MEDIA_MOUNTED.equals(android.os.Environment
				.getExternalStorageState())){
			DebugLog.i(TAG,"MEDIA_MOUNTED");
			updateDir=newFile(Environment.getExternalStorageDirectory(),
					Global.downloadDir);
			updateFile=newFile(updateDir.getPath(),titleId+".apk");
//titleID是文件名
		}else{
updateDir=newFile(Environment.getDataDirectory().toString());
//获取"/data"路径,问什么是/data,下面会说。
		DebugLog.i(TAG,"Environment="+Environment.getDataDirectory().toString());
updateFile=newFile(updateDir.getPath(),titleId+".apk");
DebugLog.i(TAG,"updateFile="+updateFile.toString());
}


3、为什么在没有SD卡的情况下是“/data”

如下图,是查看了手机内置存储的路径权限一表,是系统的文件夹的只有/data。其他都要root。






4、确定了路径就是显示一个NotificationManager,来看一下下载的进度,并且启动一个线程来下载;

this.updateNotificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
this.updateNotification=newNotification();
//点击通知栏以后自动消失的标记
this.updateNotification.flags|=Notification.FLAG_AUTO_CANCEL;
//设置下载过程中,点击通知栏,回到主界面
updateIntent=newIntent(this,O2oMainActivity.class);
updatePendingIntent=PendingIntent.getActivity(this,0,updateIntent,
0);
//设置通知栏显示内容
updateNotification.icon=R.drawable.logo;

updateNotification.tickerText="开始下载";
updateNotification.setLatestEventInfo(this,R.string.app_name+"","0%",
updatePendingIntent);
//发出通知

updateNotificationManager.notify(0,updateNotification);
//开启一个新的线程下载,如果使用Service同步下载,会导致ANR问题,Service本身也会阻塞
newThread(newUpdateRunnable()).start();//这个是下载的重点,是下载的过程

线程代码:

//这里有最关键的几个步骤

<preclass="html"name="code">	classUpdateRunnableimplementsRunnable{
Messagemessage=updateHandler.obtainMessage();

publicvoidrun(){
message.what=DOWNLOAD_COMPLETE;
try{
//增加权限<uses-permission
//android:name="android.permission.WRITE_EXTERNAL_STORAGE">;
			if(!updateDir.exists()){
updateDir.mkdirs();
}
if(!updateFile.exists()){
updateFile.createNewFile();
}
setUpdateDir(updateFile);//这个在创建了下载了这个文件就改变了它的权限,apk就可以执行安装了;</span>
				//增加权限<uses-permission
//android:name="android.permission.INTERNET">;
longdownloadSize=downloadUpdateFile(
downloadUrl,
updateFile);
if(downloadSize>0){
//下载成功
updateHandler.sendMessage(message);
}
}catch(Exceptionex){
ex.printStackTrace();
message.what=DOWNLOAD_FAIL;
//下载失败
updateHandler.sendMessage(message);
}
}
}


4、其他的下载代码我就不写下去了,最关键的也就几行代码而已,其他的其他高手也写过了;

下载、自动识别安装的过程都在一个文件UpdateVersionService.java

最后别忘了如下如下代码打开:

<service
android:name="com.o2o.uitl.UpdateVersionService"
android:exported="false"/>
 [/code]
	IntentupdateIntent=newIntent(O2oMainActivity.this,UpdateVersionService.class
O2oMainActivity.this.startService(updateIntent);


写代码时有的代码参考了网上的代码,如有涉及版权请提醒;

UpdateVersionService.java百度网盘下载如下:

链接:http://pan.baidu.com/s/1jGs5ZCE密码:0iuy
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: