1

无法让状态栏通知工作......我按照指南,并在中创建了以下代码AsyncTask,它是从doInBackground()方法调用的:

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()MainActivity

出于某种原因,当我使用参数 1 调用它时,什么也没有发生。

@Override
protected String[] doInBackground(String... empty) {
    newNotification(1);
    return null;
}   

调试显示所有代码行都在运行,但没有出现任何通知。我没有发现任何关于特殊权限的提及,或者它需要从任何地方的 UI 线程运行。我认为它不会因为后台服务旨在通知......就像消息到达时一样。

任何想法我做错了什么?

4

2 回答 2

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());
于 2013-05-09T21:57:09.527 回答
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);
    }
}

}

于 2013-05-09T22:36:49.583 回答