I want to develop a download project library for my android applications. I created a singleton class that takes an object -including url of file that should be download and a compactNotificationBuilder object - and then add it to the download queue. When download start it will show the progress into the notification bar.
I use below code in my library:
public void initialize(Context context)
{
//------> set other private variable
this.appContext= context;
this.applicationPackageName= context.getApplicationContext().getPackageName();
mNotifyManager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
}
private void downloadFile(DownloadToken token){
try{
this.mBuilder= token.getnCompactBuilder();
new Downloader().execute(token.getTargetUrl());
}catch(Exception ex){
ex.printStackTrace();
}
}
private class Downloader extends AsyncTask<String, Integer, Integer> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Displays the progress bar for the first time.
mBuilder.setProgress(100, 0, false);
mNotifyManager.notify(id, mBuilder.build());
}
@Override
protected void onProgressUpdate(Integer... values) {
// Update progress
mBuilder.setProgress(100, values[0], false);
mNotifyManager.notify(id, mBuilder.build());
super.onProgressUpdate(values);
}
@Override
protected Integer doInBackground(String... params) {
int count;
try {
URL url = new URL(params[0]);
URLConnection conection = url.openConnection();
conection.connect();
// getting file length
int lenghtOfFile = conection.getContentLength();
// input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream to write file
String path= Environment.getExternalStorageDirectory().getPath() + "/downloadedfile.file";
OutputStream output = new FileOutputStream(path );
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress((int)((total*100)/lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
//change text of progress bar
mBuilder.setContentText("Download complete");
// Removes the progress bar
mBuilder.setProgress(0, 0, false);
mNotifyManager.notify(id, mBuilder.build());
//remove head item from download queue
downloadQueue.remove(0);
}
}
}
and this code in my target project:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b= (Button) findViewById(R.id.downloadBtn);
b.setOnClickListener(this);
DownloadManager.getInstance().initialize(this);
}
@Override
public void onClick(View arg0) {
//creating notification intent
PendingIntent pendingIntent = PendingIntent.getActivity(
this,
0,
new Intent(this, MainActivity.class),
0
);
//creating notification builder
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentTitle("mytitle")
.setContentText("mytext")
.setSmallIcon(R.id.icon)
.setContentIntent(pendingIntent);
//creating download token
DownloadToken token = new DownloadToken(
"myurl",
mBuilder
);
//adding download token to download list
DownloadManager.getInstance().addToQueue(token);
}
but when i run the app it crash on the onPreExecute method and line mNotifyManager.notify(id, mBuilder.build());
actually it open a class named ActivityThread and highlighted this line:
Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "broadcastPackage");
does any body have any idea for fixing it?
0 comments:
Post a Comment