通知错误FLAG_ACTIVITY_NEW_TASK

时间:2017-02-21 02:50:21

标签: java android

我正在接受一个我以前没有经历过的恐怖。我想要一个基本通知来显示何时执行AlarmReciever.java。该错误适用于FLAG_ACTIVITY_NEW_TASK。

任何人都可以帮忙解决问题吗?

谢谢!

AlarmReceiver:

package servicealarmdemo.test2;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

private static final int MY_NOTIFICATION_ID=1;
NotificationManager notificationManager;
Notification myNotification;
private final String myBlog = "http://android-er.blogspot.com/";

@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Alarm received!", Toast.LENGTH_LONG).show();

    Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, myIntent,Intent.FLAG_ACTIVITY_NEW_TASK);

    myNotification = new NotificationCompat.Builder(context)
            .setContentTitle("Exercise of Notification!")
            .setContentText("http://android-er.blogspot.com/")
            .setTicker("Notification!")
            .setWhen(System.currentTimeMillis())
            .setContentIntent(pendingIntent)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setAutoCancel(true)
            .build();

    notificationManager =
            (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}

}

1 个答案:

答案 0 :(得分:1)

public static PendingIntent getActivity (Context context, int requestCode, Intent intent, int flags){
    ...
}

最后一个参数可能是FLAG_ONE_SHOT, FLAG_NO_CREATE, FLAG_CANCEL_CURRENT, FLAG_UPDATE_CURRENT,或者是由Intetent.fillIn()支持的任何标志,用于控制实际发送时可以提供的意图的哪些未指定部分。 所以你的代码可能是这样的:

Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myBlog));
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, myIntent,PendingIntent.FLAG_ONE_SHOT);