gpt4 book ai didi

android - 如何在通知区域显示进度条?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:54:45 27 4
gpt4 key购买 nike

您好,我正在做一个 android 应用程序,其中我正在将视频上传到 PHP 服务器。
我正在使用 HTTPURLConnection 进行上传。我对在通知区域显示进度条并更新它感到震惊。
我搜索了将近一周的时间来做这个。但找不到提示。如果有人知道请帮助我:

我的代码:

HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead;
byte[] buffer;
String urlString = "http://xxxxx/My_path.php";
try {
long total = 0;
int count = 0;
// ------------------ CLIENT REQUEST
UUID uniqueKey = UUID.randomUUID();
fname = uniqueKey.toString();
Log.e("UNIQUE NAME", fname);
FileInputStream fileInputStream = new FileInputStream(
new File(selectedPath));
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
int length=selectedPath.length();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
+ fname + "" + lineEnd);
dos.writeBytes(lineEnd);
buffer = new byte[8192];
bytesRead = 0;
while ((bytesRead = fileInputStream.read(buffer)) > 0) {
dos.write(buffer, 0, bytesRead);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
Log.e("Debug", "File is written");
fileInputStream.close();
dos.flush();
dos.close();

} catch (MalformedURLException ex) {
Log.e("Debug", "error: " + ex.getMessage(), ex);
} catch (IOException ioe) {
Log.e("Debug", "error: " + ioe.getMessage(), ioe);
}
// ------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream(conn.getInputStream());
String str;
while ((str = inStream.readLine()) != null) {
Log.e("Debug", "Server Response " + str);
}
inStream.close();

} catch (IOException ioex) {
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}

最佳答案

经过长时间的搜索,我完成了在通知区域显示进度条的任务。我们所需要的只是asynctask。可能这里显示的代码 iam 无法完美运行,它在 iam 测试时运行良好.如果效果好,请检查并赞成这个答案。

我的代码:

public class loadVideo extends AsyncTask<Void, Integer, Void> {

int progress = 0;
Notification notification;
NotificationManager notificationManager;
int id = 10;

protected void onPreExecute() {

}

@Override
protected Void doInBackground(Void... params) {
HttpURLConnection conn = null;
DataOutputStream dos = null;
DataInputStream inStream = null;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
int bytesRead;
int sentData = 0;
byte[] buffer;
String urlString = "http://xxxxx/xxx/xxxxxx.php";
try {
UUID uniqueKey = UUID.randomUUID();
fname = uniqueKey.toString();
Log.e("UNIQUE NAME", fname);
FileInputStream fileInputStream = new FileInputStream(new File(
selectedPath));
int length = fileInputStream.available();
URL url = new URL(urlString);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
+ fname + "" + lineEnd);
dos.writeBytes(lineEnd);
buffer = new byte[8192];
bytesRead = 0;
while ((bytesRead = fileInputStream.read(buffer)) > 0) {
dos.write(buffer, 0, bytesRead);
sentData += bytesRead;
int progress = (int) ((sentData / (float) length) * 100);
publishProgress(progress);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
Log.e("Debug", "File is written");
fileInputStream.close();
dos.flush();
dos.close();

} catch (MalformedURLException ex) {
Log.e("Debug", "error: " + ex.getMessage(), ex);
} catch (IOException ioe) {
Log.e("Debug", "error: " + ioe.getMessage(), ioe);
}
// ------------------ read the SERVER RESPONSE
try {
inStream = new DataInputStream(conn.getInputStream());
String str;
while ((str = inStream.readLine()) != null) {
Log.e("Debug", "Server Response " + str);
}
inStream.close();

} catch (IOException ioex) {
Log.e("Debug", "error: " + ioex.getMessage(), ioex);
}

return null;
}

@Override
protected void onProgressUpdate(Integer... progress) {

Intent intent = new Intent();
final PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(), 0, intent, 0);
notification = new Notification(R.drawable.video_upload,
"Uploading file", System.currentTimeMillis());
notification.flags = notification.flags
| Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(getApplicationContext()
.getPackageName(), R.layout.upload_progress_bar);
notification.contentIntent = pendingIntent;
notification.contentView.setImageViewResource(R.id.status_icon,
R.drawable.video_upload);
notification.contentView.setTextViewText(R.id.status_text,
"Uploading...");
notification.contentView.setProgressBar(R.id.progressBar1, 100,
progress[0], false);
getApplicationContext();
notificationManager = (NotificationManager) getApplicationContext()
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, notification);
}

protected void onPostExecute(Void result) {
Notification notification = new Notification();
Intent intent1 = new Intent(MultiThreadActivity.this,
MultiThreadActivity.class);
final PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(), 0, intent1, 0);
int icon = R.drawable.check_16; // icon from resources
CharSequence tickerText = "Video Uploaded Successfully"; // ticker-text
CharSequence contentTitle = getResources().getString(
R.string.app_name); // expanded message
// title
CharSequence contentText = "Video Uploaded Successfully"; // expanded
// message
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application
// Context
notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, contentTitle, contentText,
pendingIntent);
String notificationService = Context.NOTIFICATION_SERVICE;
notificationManager = (NotificationManager) context
.getSystemService(notificationService);
notificationManager.notify(id, notification);
}
}

感谢和问候桑达尔。

关于android - 如何在通知区域显示进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12128121/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com