Custom status bar notification for background download

╄→гoц情女王★ 提交于 2019-12-06 07:24:32

Hmm... Well I compared your code to my code that already works... and I don't see many differences... But, it is possible that one of these minor differences is important.

final Notification notification = new Notification(R.drawable.icon, "Downloading", System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;

notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.download_progress);

notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_status);
notification.contentView.setTextViewText(R.id.status_text, "Downloading in progress");
notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);
Intent notificationIntent = new Intent(MainPage.mainActivity, MainPage.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainPage.mainActivity, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;

//getApplicationContext();
final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(
                Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_MESSAGE, notification);

First, I looked at your old code and noticed that the NOTIF_ID = 1 I'm not so sure that is a good idea because what if someone else has an ID of one. Of course I could be mistaken about that, but I just pounded in a number like 792489743 and I expect no one else would have the same number. Just a precaution I suppose.

Second, I didn't get to see if the resources were correct? What does the stack trace say? I suppose that it would've just quit out on it if there was a problem there though.

Third, I put my in its own task as Service kinda as follows

public class DownloadService extends IntentService {
    //initializing code and stuff
    private class DownloadTask extends AsyncTask<String, Void, Boolean> {

and I did it in the doInBackground This way if the user kills the app or what not it wouldn't kill the download.

Lastly, I've never used apply I don't personally see how it would hurt, but I haven't seen an example that uses it either.

Hope this helps some!

It was an emulator problem after all.....

It lagged when I "dragged down" the notification! I killed some CPU extensive processes on my PC resulting to a faster emulator.

Lesson learned. Leave the heavy multitasking to pros or to another PC.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!