Intent service = new Intent(this, DownloadService.class);
service.putExtra(DownloadService.INTENT_URL, dl);
startService(service);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(dl));
startActivity(intent);
}
}
}
- 下载模块
package com.example.updataapk;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.RemoteViews;
public class DownloadService extends Service {
// notification 名字
private String notify_name = “apk正在下载…”;
public static final String INTENT_URL = “url”;
private Context mContext = this;
Notification mNotification;
private static final int NOTIFY_ID = 0;
private NotificationManager mNotificationManager;
/* 下载包安装路径 */
private static final String savePath = Function_Utility.getUpgradePath();
private static final String saveFileName = savePath + “demo.apk”;
private String apkUrl;
private int progress;
boolean canceled;
private Thread downLoadThread;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
}
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(“DownloadServi