2014-09-10 54 views
0

我成功地創建了啓動一個服務,它通過轉這樣表示恢復應用

public int onStartCommand(Intent intent, int flags, int startId){ 

String notificationText = "working" 
     myNotification = new NotificationCompat.Builder(getApplicationContext()) 
       .setContentTitle("test") 
       .setContentText(notificationText) 
       .setTicker("Notification!") 
       .setWhen(System.currentTimeMillis()) 
       .setDefaults(Notification.DEFAULT_SOUND) 
       .setAutoCancel(true) 
       .setSmallIcon(R.drawable.ic_launcher) 
       .build(); 

//FOLLOWING TECHNIQUE IS DEPRECATED 
/* PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 
    myNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); */ 

     myNotification.flags |= Notification.FLAG_NO_CLEAR; 
     myNotification.flags = Notification.FLAG_ONGOING_EVENT; 
     notificationManager.notify(1, myNotification); 

     return START_STICKY; 
} 

我知道有與此相關的其他問題,但它們都使用的通知活動setLatestEventInfo它被廢棄

這是我從另一個活動調用:

protected void beginBackgroundSer(){ 

     intentService = new Intent(this, Service.class); 
     intentService.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
     this.startService(intentService); 

    } 

所以我想要的是簡歷每當我點擊通過非棄用方法的活動。非常感謝您的幫助。

回答

0

您可以通過以下步驟創建一個通知。

  1. 創建通知ID以供將來參考,以通過通知管理器更新通知。 // Notification ID to allow for future updates private static final int MY_NOTIFICATION_ID = 1;
  2. 爲通知創建文本元素。

    // Notification Text Elements 
    private final CharSequence tickerText = "This is a Really, Really, Super Long Notification Message!"; 
    private final CharSequence contentTitle = "Notification"; 
    private final CharSequence contentText = "You've Been Notified!"; 
    
  3. 定義通知動作要素,即當用戶點擊在通知抽屜什麼動作發生的觀點?我們使用意圖來處理這個問題。

    // Notification Action Elements 
    private Intent mNotificationIntent; 
    private PendingIntent mContentIntent; 
    
  4. 您還可以定義此通知的聲音和振動。

    // Notification Sound and Vibration on Arrival 
    private Uri soundURI = Uri 
         .parse("android.resource://com.examples.Notification.StatusBarWithCustomView/" 
           + R.raw.alarm_rooster); 
    private long[] mVibratePattern = { 0, 200, 200, 300 }; 
    
  5. 使用Notification.Builder類創建通知。在您的onCreate()

    mNotificationIntent = new Intent(getApplicationContext(), 
         YourActivity.class); 
    mContentIntent = PendingIntent.getActivity(getApplicationContext(), 0, 
         mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 
    
    
          // Build the Notification 
    
          Notification.Builder notificationBuilder = new Notification.Builder(
            getApplicationContext()) 
           .setTicker(tickerText) 
           .setSmallIcon(android.R.drawable.stat_sys_warning) 
           .setAutoCancel(true) 
           .setContentIntent(mContentIntent) 
           .setSound(soundURI) 
           .setVibrate(mVibratePattern) 
           .setContent(mContentView); 
    
          // Pass the Notification to the NotificationManager: 
          NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
          mNotificationManager.notify(MY_NOTIFICATION_ID, 
            notificationBuilder.build()); 
    
0

documentations

公共NotificationCompat.Builder setContentIntent(的PendingIntent意圖) - 提供一個的PendingIntent點擊通知時發送。如果您沒有提供意圖,現在可以通過調用RemoteViews.setOnClickPendingIntent(int,PendingIntent)將PendingIntents添加到單擊的視圖中以啓動它們。請務必閱讀Notification.contentIntent以瞭解如何正確使用它。

所以,你可以使用setContentIntent(PendingIntent intent)方法類似

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0); 
myNotification = new NotificationCompat.Builder(getApplicationContext()) 
      .setContentTitle("test") 
      .setContentText(notificationText) 
      .setTicker("Notification!") 
      .setWhen(System.currentTimeMillis()) 
      .setDefaults(Notification.DEFAULT_SOUND) 
      .setAutoCancel(true) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContentIntent(contentIntent) //added this new line 
      .build(); 

notificationManager.notify(1, myNotification); 
+0

當我點擊它會提醒「很抱歉,但應用程序已中斷」的通知執行此操作。任何想法爲什麼? – 2014-09-10 22:40:51

+0

我看到你接受了一個答案,但你是否得到了你在做錯什麼?這很重要。你需要知道爲什麼它說「我們很抱歉,但應用程序已被中斷」。 – 2014-09-10 22:54:52