我想progressbar
在所有 android 平台的通知中显示。我使用远程视图progress bar
在 android 中显示。我已经在我的项目中添加了android-support-v4库。如果我替换
mBuilder = new NotificationCompat.Builder(MainActivity.this);
notification = mBuilder.build();
至
notification = new Notification(R.drawable.notification, "simulating a upload", System .currentTimeMillis());
然后通知正在显示,但它是不推荐使用的方法。
这是代码:
class SendDatabaseToServer extends
AsyncTask<ArrayList<LogFishMetadata>, Integer, String> {
private NotificationCompat.Builder mBuilder;
private NotificationManager mNotifyManager;
private RemoteViews remoteViews;
private Intent intent;
private PendingIntent pendingIntent;
private Notification notification;
private int notificationId = 1;
int start;
int percentage;
int inc;
public SendDatabaseToServer(int value) {
// TODO Auto-generated constructor stub
logout = value;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(MainActivity.this, MainActivity.class);
pendingIntent = PendingIntent.getActivity(MainActivity.this,
notificationId, intent, 0);
remoteViews = new RemoteViews(getPackageName(),
R.layout.notification_downloader);
mBuilder = new NotificationCompat.Builder(MainActivity.this);
notification = mBuilder.build();
//notification = new Notification(R.drawable.notification, "simulating a upload", System.currentTimeMillis());
notification.contentIntent = pendingIntent;
notification.flags = notification.flags
| Notification.FLAG_ONGOING_EVENT;
notification.contentView = remoteViews;
notification.contentView.setTextViewText(R.id.timeStamp, DateTime.getStringFromDate(new Date()));
notification.contentView.setTextViewText(R.id.progress,
"UPloading....");
notification.contentView.setProgressBar(R.id.progressBar, 100, 0,
false);
mNotifyManager.notify(notificationId, notification);
}
@Override
protected String doInBackground(ArrayList<LogFishMetadata>... params) {
// TODO Auto-generated method stub
int count = params[0].size();
percentage=100/count;
for (int i = 0; i < params[0].size(); i++) {
System.out.println("........." + i);
start=i*percentage;
inc=i+1;
try {
database.updateServerdata(params[0].get(i));
for(int j=start;j<=inc*percentage;j++)
{
publishProgress(j);
}
} catch (Exception e) {
// TODO: handle exception
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
// progressBar.setProgress(values[0]);
System.out.println("values......." + values[0]);
if(values[0]>=100)
{
values[0]=98;
}
notification.contentView.setProgressBar(R.id.progressBar, 100,
values[0], false);
mNotifyManager.notify(notificationId, notification);
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
notification.contentView.setTextViewText(R.id.progress,
"UPloading Complete");
notification.contentView.setProgressBar(R.id.progressBar,100, 100,
false);
mNotifyManager.notify(notificationId, notification);
if (logout == 2) {
System.out.println(" send Aler dialog....................................");
logoutConfirmationDialog("Sync Complete; Do you still want to logout?");
}
System.out.println("complete");
}
}