无法让状态栏通知生效...我按照this指南,在AsyncTask
中创建了以下代码,它是从doInBackground()
方法调用的:< / p>
private void newNotification(int count) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(handler.getContext());
builder.setContentTitle("New Notification");
builder.setContentText("Testing notification");
builder.setNumber(count);
// Creates an explicit intent for an Activity in your app
Intent intent = new Intent(handler.getContext(), MainActivity.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(handler.getContext());
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) handler.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
handler
是对MainActivity
的引用,使用实现getContext()
的接口,该接口返回this.getContext()
的{{1}}
出于某种原因,当我用参数1调用它时,没有任何反应。
MainActivity
Debug显示代码的所有行都在运行,但不显示任何通知。我没有发现任何特殊权限的提及,或者它需要从任何地方的UI线程运行。我认为它没有,因为后台服务意味着通知...就像消息到达时一样。
任何想法我做错了什么?
答案 0 :(得分:0)
1)您是否尝试过从UI线程发帖?
2)这是因为默认情况下服务在UI线程中运行,除非明确地放在一个单独的线程中。
3)为什么不首先尝试一个简单的例子,如:
NotificationCompat.Builder builder = new NotificationCompat.Builder(handler.getContext());
builder.setContentTitle("New Notification");
builder.setContentText("Testing notification"); builder.setNumber(count);
builder.setSmallIcon(R.drawable.ic_launcher);
Intent notificationIntent = new Intent(this, YourClass.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), "hello", "hello", contentIntent);
mNotificationManager.notify(1, builder.build());
答案 1 :(得分:0)
我在我的任务中使用通知来下载文件,查看其状态,最后提供通知以使用pdf查看器打开文件。它可能对你有帮助。
public class MyTask extends AsyncTask<Void, Void, String>
{
int NOTIFICATION_ID = (int) System.currentTimeMillis();
NotificationManager notificationManager;
Notification notification;
public MyTask() {
}
protected void onPreExecute()
{
}
protected String doInBackground(Void... params)
{
[...]
// Set initial notification
notification = new Notification(R.drawable.ic_action_export_statusbar, "Download of " + downloadSubject + " started...",
System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL | Notification.FLAG_ONLY_ALERT_ONCE;
notification.contentView = new RemoteViews(appContext.getPackageName(), R.layout.download_progress);
final PendingIntent pendingIntent = PendingIntent.getActivity(appContext, 0, new Intent(), 0);
notification.contentIntent = pendingIntent;
notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_action_export_statusbar);
notification.contentView.setTextViewText(R.id.status_text, "Download of " + downloadSubjectShort + " in progress...");
notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);
notificationManager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
[...]
}
protected void onCancelled()
{
// remove the notification
notificationManager.cancel(NOTIFICATION_ID);
}
protected void onPostExecute(String pdfPath)
{
// remove the notification
notificationManager.cancel(NOTIFICATION_ID);
if (pdfPath != null)
{
// show final notification to open pdf
notification = new Notification(R.drawable.ic_action_export_finished_statusbar, "Download of " + downloadSubject
+ " finished...", System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL;
Intent notifyIntent = new Intent();
notifyIntent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(pdfPath);
notifyIntent.setDataAndType(Uri.fromFile(file), "application/pdf");
notification.setLatestEventInfo(appContext, "Download finished", "open downloaded file",
PendingIntent.getActivity(appContext, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK));
notificationManager.notify(NOTIFICATION_ID + 1, notification);
} else
{
// show final notification to inform about failure
notification = new Notification(R.drawable.ic_action_export_failed_statusbar, "Error in download",
System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(appContext, "Download failed", "Error",
PendingIntent.getActivity(appContext, 0, new Intent(), android.content.Intent.FLAG_ACTIVITY_NEW_TASK));
notificationManager.notify(NOTIFICATION_ID + 1, notification);
}
}
}