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());
}

handlerMainActivityを実装するインターフェースを使用した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 に答える