我正在开发一个带有服务的应用程序,该服务显示通知区域中的计时器的进度(带有进度条和文本).我已经在下面提取了一个具有相同问题的简单示例.
服务代码:
public class TNService extends Service {
private NotificationManager nm;
private Notification notification;
private RemoteViews remoteView;
@Override
public void onCreate () {
nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(android.R.drawable.stat_sys_download,
"My notification",
System.currentTimeMillis());
remoteView = new RemoteViews(this.getPackageName(),
R.layout.notification);
remoteView.setImageViewResource(R.id.icon, android.R.drawable.stat_sys_download);
remoteView.setTextViewText(R.id.text, "");
remoteView.setProgressBar(R.id.progress, 100, 0, false);
notification.flags = Notification.FLAG_NO_CLEAR;
notification.contentView = remoteView;
notification.contentIntent = PendingIntent.getActivity(this, 0, new Intent(this,
TNActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
Timer timer = new Timer ();
timer.schedule(new TNTask(this), 0, 200);
}
@Override
public IBinder onBind(Intent arg0) {
return null; …Run Code Online (Sandbox Code Playgroud) 我目前有一个asynctask从服务器下载mp3.当用户开始下载时,会创建状态栏通知.这将实时显示下载进度.我唯一担心的是手机放慢了几乎.有没有办法延迟显示的进度或使我的代码更快的方法?谢谢.
代码如下:
public class DownloadFile extends AsyncTask<String, String, String> {
CharSequence contentText;
Context context;
CharSequence contentTitle;
PendingIntent contentIntent;
int HELLO_ID = 1;
long time;
int icon;
CharSequence tickerText;
File file;
public void downloadNotification() {
String ns = Context.NOTIFICATION_SERVICE;
notificationManager = (NotificationManager) getSystemService(ns);
icon = R.drawable.sdricontest;
//the text that appears first on the status bar
tickerText = "Downloading...";
time = System.currentTimeMillis();
notification = new Notification(icon, tickerText, time);
context = getApplicationContext();
//the bold font
contentTitle = "Your download is in …Run Code Online (Sandbox Code Playgroud)