升级提示及加载

//***************************************************************

//*****************    启动类               ***************************
//***************************************************************

 //升级
 protected void update() {
  // 新版
  Message msg = BroadcastHandler.obtainMessage();
  BroadcastHandler.sendMessage(msg);
 }

 protected void updateAlertDialog() {
  String str_UpdateMessage = "";
  if(ConstMethod.b_forceToUpdate){
   str_UpdateMessage = "很抱歉,当前版本已被停用。" + "\n" + "请您升级为最新版本("
   + Account.ver + ")" + "。" + "\n" + "描述:" + "\n" + Account.desc;
  }else if(ConstMethod.b_update){
   str_UpdateMessage = "当前版本还可用,目前最新的版本为:" + Account.ver + "。"
   + "\n" + "版本描述:" + "\n" + Account.desc;
  }
  myDialog = new AlertDialog.Builder(this).create();
  myDialog.show();
  myDialog.getWindow().setContentView(R.layout.update);
  myDialog.setCancelable(false);
  Button btn_ok = (Button) myDialog.findViewById(R.id.btn_ok);
  Button btn_cancel = (Button) myDialog.findViewById(R.id.btn_cancel);
  TextView tv_content = (TextView) myDialog.findViewById(R.id.tv_content);
  final CheckBox cb_tip = (CheckBox) myDialog.findViewById(R.id.cb_notip);
  if(ConstMethod.b_forceToUpdate){
   cb_tip.setVisibility(View.GONE);
  }
  tv_content.setText(str_UpdateMessage);
  btn_ok.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // 取得欲安装程序之文件名称
    fileEx = Account.url.substring(
      Account.url.lastIndexOf(".") + 1, Account.url.length())
      .toLowerCase();
    fileNa = Account.url.substring(
      Account.url.lastIndexOf("/") + 1,
      Account.url.lastIndexOf("."));
    Beginning();
    myDialog.dismiss();
    if (!ConstMethod.b_forceToUpdate&&cb_tip.isChecked()) {
     share.edit()
       .putString(ConstMethod.SHARE_VERSION, Account.ver)
       .commit();
     share.edit().putBoolean(ConstMethod.SHARE_NO_UPDATE, true)
       .commit();
    }
   }
  });

  btn_cancel.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    myDialog.dismiss();
    if (!ConstMethod.b_forceToUpdate&&cb_tip.isChecked()) {
     share.edit()
       .putString(ConstMethod.SHARE_VERSION, Account.ver)
       .commit();
     share.edit().putBoolean(ConstMethod.SHARE_NO_UPDATE, true)
       .commit();
    }
   }
  });
 }

 // 升级
 protected Handler BroadcastHandler = new Handler() {
  public void handleMessage(Message msg) {
   updateAlertDialog();
  }
 };

 protected void Beginning() {
  LinearLayout ll = (LinearLayout) LayoutInflater.from(this)
    .inflate(R.layout.layout_loadapk, null);

  pb = (ProgressBar) ll.findViewById(R.id.down_pb);
  tv = (TextView) ll.findViewById(R.id.tv);
  Builder builder = new Builder(this);
  builder.setView(ll);
  builder.setTitle("版本更新进度提示");
  builder.setNegativeButton("后台下载",
    new DialogInterface.OnClickListener() {
     @Override
     public void onClick(DialogInterface dialog, int which) {
      Intent intent = new Intent(NetworkActiviy.this,VersionService.class);
      startService(intent);
      dialog.dismiss();
     }
    });

  builder.show();
  new Thread() {
   public void run() {
    loadFile(Account.url);
   }
  }.start();
 }

 protected void loadFile(String url) {
  HttpClient client = new DefaultHttpClient();
  HttpGet get = new HttpGet(url);
  HttpResponse response;
  try {
   response = client.execute(get);

   HttpEntity entity = response.getEntity();
   float length = entity.getContentLength();

   InputStream is = entity.getContent();
   FileOutputStream fileOutputStream = null;
   if (is != null) {
    File file = new File(Environment.getExternalStorageDirectory(),
      fileNa + "." + fileEx);
    fileOutputStream = new FileOutputStream(file);
    byte[] buf = new byte[1024];
    int ch = -1;
    float count = 0;
    while ((ch = is.read(buf)) != -1) {
     fileOutputStream.write(buf, 0, ch);
     count += ch;
     sendMsg(1, (int) (count * 100 / length));
    }
   }
   sendMsg(2, 0);
   fileOutputStream.flush();
   if (fileOutputStream != null) {
    fileOutputStream.close();
   }
  } catch (Exception e) {
   sendMsg(-1, 0);
  }
 }

 protected void sendMsg(int flag, int c) {
  Message msg = new Message();
  msg.what = flag;
  msg.arg1 = c;
  handler.sendMessage(msg);
 }

 protected Handler handler = new Handler() {
  @Override
  public void handleMessage(Message msg) {// 定义一个Handler,用于处理下载线程与UI间通讯
   if (!Thread.currentThread().isInterrupted()) {
    switch (msg.what) {
    case 1:
     pb.setProgress(msg.arg1);
     ConstMethod.loading_process = msg.arg1;
     tv.setText("已为您加载了:" + ConstMethod.loading_process + "%");
     break;
    case 2:
     Intent intent = new Intent(Intent.ACTION_VIEW);
     intent.setDataAndType(Uri.fromFile(new File(Environment
       .getExternalStorageDirectory(), fileNa + "."
       + fileEx)),
       "application/vnd.android.package-archive");
     startActivity(intent);
     pb.setVisibility(View.GONE);
     break;
    case -1:
     String error = msg.getData().getString("error");
     ToastInfo(error);
     break;
    }
   }
   super.handleMessage(msg);
  }
 };

 

//***************************************************************

//*****************     后台下载         ***************************
//***************************************************************

public class VersionService extends Service {
 private NotificationManager notificationMrg;
 private int old_process = 0;
 private boolean isFirstStart=false;

 public void onCreate() {
  super.onCreate();isFirstStart=true;
  notificationMrg = (NotificationManager) this.getSystemService(android.content.Context.NOTIFICATION_SERVICE);
//  System.out.println(TestVersionUpdateActivity.loading_process+"==");
  mHandler.handleMessage(new Message());
 }

 private Handler mHandler = new Handler() {
  @Override
  public void handleMessage(Message msg) {
   // 1为出现,2为隐藏
   if(ConstMethod.loading_process>99){
    notificationMrg.cancel(0);
    stopSelf();
    return;
   }
   if(ConstMethod.loading_process>old_process){
    displayNotificationMessage(ConstMethod.loading_process);
   }
   
   new Thread() {
    public void run() {
     isFirstStart=false;
     Message msg = mHandler.obtainMessage();
     mHandler.sendMessage(msg);
    }
   }.start();
   old_process =ConstMethod.loading_process;
  }
 };

 @Override
 public void onDestroy() {
  super.onDestroy();
 }

 private void displayNotificationMessage(int count) {

  // Notification的Intent,即点击后转向的Activity
  Intent notificationIntent1 = new Intent(this, this.getClass());
  notificationIntent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
  PendingIntent contentIntent1 = PendingIntent.getActivity(this, 0,
    notificationIntent1, 0);

  // 创建Notifcation
  Notification notification = new Notification(R.drawable.ic_launcher,
    "XXX程序升级", System.currentTimeMillis());// 设定Notification出现时的声音,一般不建议自定义
  if(isFirstStart || ConstMethod.loading_process>97){
   notification.defaults |= Notification.DEFAULT_SOUND;// 设定是否振动
   notification.defaults |= Notification.DEFAULT_VIBRATE;
  }notification.flags |= Notification.FLAG_ONGOING_EVENT;

  // 创建RemoteViews用在Notification中
  RemoteViews contentView = new RemoteViews(getPackageName(),
    R.layout.notification_version);
  contentView.setTextViewText(R.id.n_title,
    "升级提示");
  contentView.setTextViewText(R.id.n_text, "  当前进度:"+count+"% ");
  contentView.setProgressBar(R.id.n_progress, 100, count, false);

  notification.contentView = contentView;
  notification.contentIntent = contentIntent1;

  notificationMrg.notify(0, notification);
 }

 @Override
 public IBinder onBind(Intent intent) {
  // TODO Auto-generated method stub
  return null;
 }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 如果你在运行 "cnpm-v" 时出现了 "无法加载文件" 的错误提示,有以下几种可能的原因: 1. 文件不存在:请确认文件是否存在,如果不存在请安装。 2. 文件路径错误:请确认文件的路径是否正确,如果不正确请更正。 3. 权限问题:如果没有权限读取该文件,请确保有相应的权限。 如果以上方法都无法解决问题,可以尝试重新安装 cnpm。 如果仍然无法解决,请提供更多的细节以便更好的帮助您。 ### 回答2: 当出现"cnpm-v提示无法加载文件"的错误时,可能是由于以下原因导致的: 1. 首先,你需要检查你的电脑上是否已经安装了cnpm,cnpm是一个Node.js的包管理工具,它是在npm的基础上进行了优化和改进。如果你没有安装cnpm,你需要使用以下命令来安装它: ``` npm install -g cnpm ``` 2. 如果你已经安装了cnpm,但是在使用cnpm-v命令时还是提示无法加载文件,可能是因为你的系统环境变量没有正确配置。你可以通过以下步骤来检查和设置环境变量: - 首先,打开命令提示符或终端窗口。 - 输入以下命令来查看当前系统的环境变量: ``` echo %PATH% ``` - 如果你发现环境变量中没有包含cnpm的安装路径,你需要手动将cnpm的安装路径添加到环境变量中。例如,如果你的cnpm安装在C:\Users\YourUserName\AppData\Roaming\npm目录下,你需要将这个路径添加到环境变量中。 - 打开“系统属性”->“高级系统设置”->“环境变量”,在“系统变量”中找到“Path”变量并编辑它。 - 将cnpm的安装路径添加到“Path”变量的值中,使用分号(;)分隔每个路径。 - 确保保存更改,并关闭所有打开的命令提示符或终端窗口。然后重新打开一个窗口,尝试再次运行cnpm-v命令。 3. 如果以上方法都无效,可能是由于cnpm的安装文件损坏或被删除导致的。此时,你可以尝试重新安装cnpm。先卸载现有的cnpm,然后再重新按照步骤1中的方法进行安装。 希望以上解答对你有帮助! ### 回答3: cnpm-v提示无法加载文件一般是由以下几个原因导致的: 1. 安装cnpm的命令行工具不正确:cnpm是一个Node.js的包管理工具,要正确使用cnpm-v命令,首先需要确保已经安装了Node.js,并且正确配置了环境变量。如果没有正确安装或配置,就会导致cnpm-v命令无法识别。需要检查Node.js的安装路径和环境变量配置是否正确。 2. cnpm工具没有正确安装:如果cnpm-v命令提示无法加载文件,可能是因为cnpm工具没有正确安装。尝试重新安装cnpm,可以使用以下命令: ``` npm install -g cnpm --registry=https://registry.npm.taobao.org ``` 这会通过npm全局安装cnpm,并指定了淘宝的镜像源。安装完成后,再次尝试使用cnpm-v命令查看版本。 3. cnpm工具版本过低或过高:如果cnpm版本过低或过高,可能会导致无法加载文件的错误。可以尝试更新cnpm版本,使用以下命令更新: ``` cnpm install -g cnpm ``` 这会使用cnpm自身的更新命令将cnpm工具更新到最新版本。更新完成后,再次尝试使用cnpm-v命令查看版本。 4. 其他问题:如果以上方法仍然无法解决问题,可能是由于其他原因导致的。可以尝试查看错误提示的详细信息,或者在技术社区或论坛上提问寻求帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值