Java 通知错误标志\u活动\u新建\u任务
我目前正在接受一个我以前从未经历过的性爱。我希望在执行AlarmReceiver.java时显示一个基本通知。该错误适用于标记\活动\新\任务 有人能帮忙解决吗 谢谢大家! 报警接收器:Java 通知错误标志\u活动\u新建\u任务,java,android,Java,Android,我目前正在接受一个我以前从未经历过的性爱。我希望在执行AlarmReceiver.java时显示一个基本通知。该错误适用于标记\活动\新\任务 有人能帮忙解决吗 谢谢大家! 报警接收器: package servicealarmdemo.test2; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.c
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);
}
}
最后一个参数可以是FLAG\u ONE\u SHOT、FLAG\u NO\u CREATE、FLAG\u CANCEL\u CURRENT、FLAG\u UPDATE\u CURRENT
,也可以是intent.fillIn()支持的任何标志,以控制在实际发送时可以提供intent的哪些未指定部分。
因此,您的代码可以如下所示:
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);
谢谢你的回复!我刚刚用你提供的代码更新了它,但是当它试图运行通知时,警报崩溃了。与更新前的问题相同吗?如果可能,请显示日志。再次感谢您的回复。它实际上没有显示任何错误,这让我更加困惑,也让我无法弄清问题所在:我已将MainActivity附加到原始帖子上,以防在那里看到任何东西,或者应该将此意图添加到他们而不是AlarmReceiver?我输入了您的代码,发现您没有为通知设置图标。这可能就是它崩溃的原因。只需仔细阅读日志(stacktrace),它会告诉您必须做什么。如果您要在notification fire上启动某些活动,则需要向您上面提到的startActivity添加一个标志。只需将此标志添加到您的意图中即可。
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);