安卓版本更新

import android.app.Notification;
import android.app.NotificationManager;
import android.app.Service;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;
import android.widget.RemoteViews;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.zzz.drawhome.R;
import com.zzz.drawhome.application.MyApplication;

import java.io.File;

public class DownLoadServices extends Service {
    HttpUtils httpUtils = MyApplication.getApp().getHttpUtils();
    private String versionUrl;
    private NotificationManager manager;
    private Notification notification;

    public DownLoadServices() {
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (intent!=null){
            versionUrl = intent.getStringExtra("VersionUrl");
            Toast.makeText(this, "更新中。。。", Toast.LENGTH_SHORT).show();
            //下载文件,
            goUpdate();


        }

        return START_REDELIVER_INTENT;
    }


    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * 下载更新版本  , 提示用户,下载进度,通知栏通知
     */
    private void goUpdate() {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            final String file = Environment.getExternalStorageDirectory().getPath() + "/huihome.apk";
            File newFile=new File(file);
            if (newFile.exists()){
                newFile.delete();
            }
            httpUtils.download(
                    versionUrl,
                    file,
                    true,
                    new RequestCallBack<File>() {
                        @Override
                        public void onLoading(long total, long current, boolean isUploading) {
                            // 提示用户,下载进度,通知栏通知
                            startUpNotification(total, current);

                        }
                        @Override
                        public void onSuccess(ResponseInfo<File> responseInfo) {
                            Intent intent = new Intent(Intent.ACTION_VIEW);
                            intent.addCategory(Intent.CATEGORY_DEFAULT);
                            intent.setDataAndType(Uri.fromFile(responseInfo.result),
                                    "application/vnd.android.package-archive");
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                             startActivity(intent);
                            manager.cancel(99);
                            Toast.makeText(DownLoadServices.this, "下载完成", Toast.LENGTH_SHORT).show();
                            // startActivityForResult(intent, 0);// 如果用户取消安装的话,
                            // 会返回结果,回调方法onActivityResult

                        }

                        @Override
                        public void onFailure(HttpException error, String msg) {
                            Toast.makeText(DownLoadServices.this, "下载失败", Toast.LENGTH_SHORT).show();

                        }
                    }

            );
        } else {
            Toast.makeText(this, "没有挂载内存卡无法进行更新下载", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 下载的进度的通知
     */
    private void startUpNotification(long total, long current) {
        manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notification = new Notification();
        notification.flags=Notification.FLAG_AUTO_CANCEL;
        RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.welcome_notification);
        remoteViews.setTextViewText(R.id.notificationTitle, "绘装修正在下载");

        remoteViews.setTextViewText(R.id.notificationPercent, (current*100)/total+"%");

        remoteViews.setProgressBar(R.id.notificationProgress, 100, (int) ((current*100)/total), false);

        notification.contentView = remoteViews;

        notification.tickerText="正在下载";

// note.sound=Uri.parse("file:///sdcard/good.mp3");

        notification.icon=R.drawable.icon;

      /*  PendingIntent p=PendingIntent.getActivity(TestMapActivity.this, 0, new Intent(Intent.ACTION_VIEW), 0);//这个非要不可。

        note.contentIntent=p;*/



         manager.notify(99, notification);



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



xml文件


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
    >
    <!-- <ImageView
         android:id="@+id/iv_notification"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/icon"
         />
     <TextView
         android:id="@+id/tv_notification"
         android:layout_toRightOf="@id/iv_notification"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         />

     <ProgressBar
         android:layout_toRightOf="@id/iv_notification"
         android:layout_below="@id/tv_notification"
         style="?android:attr/progressBarStyleHorizontal"
         android:layout_width="match_parent"
         android:layout_marginTop="5dp"
         android:layout_height="wrap_content"
         android:id="@+id/pb_notification"

         />
 -->
    <ImageView
        android:id="@+id/notificationImage"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/icon"

        />

    <TextView
        android:id="@+id/notificationTitle"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_alignParentRight="true"

        android:layout_toRightOf="@+id/notificationImage"

        android:paddingLeft="6dp"
        android:text="okhhh"

        android:textColor="#FF000000"

        />

    <TextView
        android:id="@+id/notificationPercent"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        android:layout_below="@+id/notificationImage"

        android:paddingTop="2dp"

        android:textColor="#FF000000"
        android:text="okhhh"
        />

    <ProgressBar
        android:id="@+id/notificationProgress"

        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignLeft="@+id/notificationTitle"

        android:layout_alignParentRight="true"

        android:layout_alignTop="@+id/notificationPercent"

        android:layout_below="@+id/notificationTitle"

        android:paddingLeft="6dp"

        android:paddingRight="3dp"

        android:paddingTop="2dp"

        />

</RelativeLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值