我正在开发一个android应用程序,从服务器下载.mp3。单击下载按钮时,ProgressBar将出现,显示屏幕上的百分比。
这很好用。现在,我想通过ProgressBar显示通知,使下载在后台工作
有人能帮我找到使用服务和建筑通知的代码吗?谢谢
我的主要活动.java代码:
public class MainActivity extends Activity {
final MediaPlayer mp=new MediaPlayer();
//---------------------
Clone c=new Clone();
//------------------
// button to show progress dialog
Button btnShowProgress;
// Progress Dialog
private ProgressDialog pDialog;
ImageView my_image;
// Progress dialog type (0 - for Horizontal progress bar)
public static final int progress_bar_type = 0;
// File url to download
private static String file_url = "http://www.eecindia.co.in/up/01%20-%20Pretham%20-%20Oruthikku%20Pinnil%20[Maango.me].mp3";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// show progress bar button
btnShowProgress = (Button) findViewById(R.id.btnProgressBar);
// Image view to show image after downloading
my_image = (ImageView) findViewById(R.id.my_image);
/**
* Show Progress bar click event
* */
btnShowProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//----------------------------------------------
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Select The Extension To Download...");
builder.setItems(new CharSequence[]
{".mp3", ".mp4", ".txt", ".srt"},
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// The 'which' argument contains the index position
// of the selected item
switch (which) {
case 0:
/////////////////////////////////////////////////////////////////
File folder = new File(Environment.getExternalStorageDirectory().getPath() + "/Jithin's/");
if (!folder.exists()) {
try {
folder.mkdirs();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Default Save Path Creation Error:" + folder);
}
}
c.setA(Environment.getExternalStorageDirectory().toString() + "/Jithin's/downloadedfile.mp3");
// starting new Async Task
new DownloadFileFromURL().execute(file_url);
/////////////////////////////////////////////////////////////
break;
case 1:
////////////////////////////////////////////////////////////
File folde = new File(Environment.getExternalStorageDirectory().getPath() + "/Jithin's/");
if (!folde.exists()) {
try {
folde.mkdirs();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Default Save Path Creation Error:" + folde);
}
}
c.setA(Environment.getExternalStorageDirectory().toString() + "/Jithin's/downloadedfile.mp4");
// starting new Async Task
new DownloadFileFromURL().execute(file_url);
////////////////////////////////////////////////
break;
case 2:
File fold = new File(Environment.getExternalStorageDirectory().getPath() + "/Jithin's/");
if (!fold.exists()) {
try {
fold.mkdirs();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Default Save Path Creation Error:" + fold);
}
}
c.setA(Environment.getExternalStorageDirectory().toString() + "/Jithin's/downloadedfile.txt");
// starting new Async Task
new DownloadFileFromURL().execute(file_url);
break;
case 3:
File fol = new File(Environment.getExternalStorageDirectory().getPath() + "/Jithin's/");
if (!fol.exists()) {
try {
fol.mkdirs();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Default Save Path Creation Error:" + fol);
}
}
c.setA(Environment.getExternalStorageDirectory().toString() + "/Jithin's/downloadedfile.srt");
// starting new Async Task
new DownloadFileFromURL().execute(file_url);
break;
}
}
});
builder.create().show();
/////-------------------------------------
}
});
}
/**
* Showing Dialog
* */
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type:
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading File. Please Wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(false);
pDialog.show();
return pDialog;
default:
return null;
}
}
/**
* Background Async Task to download file
* */
class DownloadFileFromURL extends AsyncTask<String, String, String> {
/**
* Before starting background thread
* Show Progress Bar Dialog
*/
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* Downloading file in background thread
*/
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
// 6yi7 conection.connect();
conection.setRequestProperty("Accept-Encoding", "identity");
// 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
OutputStream output = new FileOutputStream(c.getA());
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;
}
/**
* Updating progress bar
*/
protected void onProgressUpdate(String... progress) {
// setting progress percentage
try {
pDialog.setProgress(Integer.parseInt(progress[0]));
} catch (Exception d) {
Log.d("Error .. ", d.getMessage());
}
}
/**
* After completing background task
* Dismiss the progress dialog
**/
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
Toast.makeText(MainActivity.this, "Downloaded Succesfully.. check Jithin's folder 2 see file...", Toast.LENGTH_LONG).show();
//my_image.setImageDrawable(Drawable.createFromPath(imagePath));
}
}
//---------------------------------
protected void onStop(){
super.onStop();
Intent intent = new Intent(MainActivity.this,MyService.class);
startService(intent);
}
}
您可以使用前台Notification
。这是你的例子演示CCD_ 2使用的项目。