2017-02-21 75 views
-2

我目前收到一个我以前没有经历过的错误。我需要一个基本的通知来显示AlarmReciever.java何时执行。该错误是针对FLAG_ACTIVITY_NEW_TASK的。通知错误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); 
} 

} 
+0

刚刚看了你的logcat(堆栈跟踪)小心它会告诉你,你必须做什么。如果你正在开始一些关于通知消息的活动,那么它需要添加一个标志给你上面提到的startActivity。只需要将此标志添加到您的意图。 –

回答

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,或任何标志的所支持byIntent.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); 
+0

感谢您的回复!我刚刚使用您提供的代码对其进行了更新,但是当它尝试运行通知时警报崩溃。 – ZenoX

+0

与之前更新相同的问题?如果可能,请显示logcat。 – xiaoyuan

+0

再次感谢您的回复。它实际上并没有显示任何错误,这是更令人困惑,并阻止我找出问题所在:http://i.imgur.com/RTDf1KS.png – ZenoX