我正在尝试从异步任务内部从url下载apk,但它无法正常工作,绝对.apk不是或部分在这里下载,我不知道为什么。进度条对话框未显示任何进度并停留在0.请帮我解决此问题。
提前谢谢!
这是我的代码:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
InterstitialAd mInterstitialAd;
ProgressDialog pDialog;
private Button downloadButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pDialog = new ProgressDialog(this);
downloadButton = (Button) findViewById(R.id.button_download);
downloadButton.setOnClickListener(this);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
showInterstitial();
}
});
}
private void showInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_download:
DownloadAndInstall downloadAndInstall = new DownloadAndInstall();
downloadAndInstall.execute("http://cygnus.androidapksfree.com/hulk/com.viber.voip_v6.0.2.22-226_Android-4.0.apk");
break;
}
}
class DownloadAndInstall extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog.setMessage("Downloading App");
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setIndeterminate(true);
pDialog.setProgress(0);
pDialog.show();
}
@Override
protected String doInBackground(String... urlArr) {
int count;
try {
Log.d("URL","URL = "+urlArr[0]);
URL url = new URL(urlArr[0]);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
int fileSize = urlConnection.getContentLength();
File sdcard = Environment.getExternalStorageDirectory();
InputStream input = new BufferedInputStream(url.openStream(), 8192);
OutputStream output = new FileOutputStream(sdcard+"/app_file.apk");
byte[] buffer = new byte[1024];
float total = 0;
while ((count = input.read(buffer)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/fileSize));
output.write(buffer, 0, count);
}
output.flush();
output.close();
input.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e){
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(String... progress) {
pDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
}
AndroidMenifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="in.friper.rajeshincorp.abc">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission
android:name="android.permission.INSTALL_PACKAGES"
tools:ignore="ProtectedPermissions"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".SplashActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="@string/app_name"
android:theme="@style/FullscreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" />
<activity
android:name=".MainActivity"
android:theme="@style/FullscreenTheme">
</activity>
</application>
SplashActivity.java
public class SplashActivity extends AppCompatActivity {
private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
private static final int MY_PERMISSIONS_REQUEST_WRITE_SYNC_SETTINGS = 345;
private static final int MY_PERMISSIONS_REQUEST_ACCESS_NETWORK_STATE = 567;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
if(ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.WRITE_SYNC_SETTINGS)!= PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.ACCESS_NETWORK_STATE)!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(SplashActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) &&
ActivityCompat.shouldShowRequestPermissionRationale(SplashActivity.this, Manifest.permission.WRITE_SYNC_SETTINGS) &&
ActivityCompat.shouldShowRequestPermissionRationale(SplashActivity.this, Manifest.permission.ACCESS_NETWORK_STATE)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(SplashActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
ActivityCompat.requestPermissions(SplashActivity.this,
new String[]{Manifest.permission.WRITE_SYNC_SETTINGS},
MY_PERMISSIONS_REQUEST_WRITE_SYNC_SETTINGS);
ActivityCompat.requestPermissions(SplashActivity.this,
new String[]{Manifest.permission.ACCESS_NETWORK_STATE},
MY_PERMISSIONS_REQUEST_ACCESS_NETWORK_STATE);
}
}
goToNextActivity();
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
} else {
// permission denied
}
return;
}
case MY_PERMISSIONS_REQUEST_WRITE_SYNC_SETTINGS: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
} else {
// permission denied
}
return;
}
case MY_PERMISSIONS_REQUEST_ACCESS_NETWORK_STATE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted
} else {
// permission denied
}
return;
}
}
}
public void goToNextActivity(){
Intent i= new Intent(this,MainActivity.class);
startActivity(i);
finish();
}
}
答案 0 :(得分:0)
上面的代码在修复了从错误的线程调用的Toast之后实际上工作正常 由于apk的大小,百分比似乎停留在0上。