2017-02-21 74 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