life02

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  197 随笔 :: 3 文章 :: 37 评论 :: 0 Trackbacks
public static void Install(Context ctx, String strLocalFile) {
        Intent intentInstall 
= new Intent();
        String apkPath 
= "/data/data/" + ctx.getPackageName() + "/files";
        String apkName 
= "yuan.apk";
        File file 
= new File(apkPath, apkName);
        
try {
//assets下对于超过2M 的文件,有所限制,建议改名为Jpg
  InputStream is 
= ctx.getAssets().open("yuan.apk");

            
if (!file.exists()) {
                file.createNewFile();
                FileOutputStream os 
= ctx.openFileOutput(file.getName(),
                        Context.MODE_WORLD_WRITEABLE);
                
byte[] bytes = new byte[512];
                
int i = -1;
                
while ((i = is.read(bytes)) > 0) {
                    os.write(bytes);
                }

                os.close();
                is.close();
                Log.e(
"""----------- has been copy to ");
            } 
else {
                Log.e(
"""-----------cunzai ");
            }
            String permission 
= "666";

            
try {
                String command 
= "chmod " + permission + " " + apkPath + "/"
                        
+ apkName;
                Runtime runtime 
= Runtime.getRuntime();
                runtime.exec(command);
            } 
catch (IOException e) {
                e.printStackTrace();
            }

        } 
catch (Exception e) {
            Log.e(
"", e.toString());
        }
        Log.e(
"""fl--" + file.getName() + "-dd---" + file.getAbsolutePath()
                
+ "-pa-" + file.getPath());
        intentInstall.setAction(android.content.Intent.ACTION_VIEW);
        intentInstall.setDataAndType(Uri.fromFile(file),
                
"application/vnd.android.package-archive");
        intentInstall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        ctx.startActivity(intentInstall);
    }

当然代码大部分参考其他人的,请见谅
同时请参考
http://ccsosnfs.iteye.com/blog/1390405

posted on 2012-03-23 10:03 life02 阅读(1021) 评论(0)  编辑 收藏 引用 所属分类: Android开发

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理