2014-07-19 41 views
1

我有一個android服務,我使用startForeground,並沒有得到我期望得到的。 我使用下面的代碼:如何使用startForeground?

Intent intent = new Intent(this, MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0, intent, 0); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 
    builder.setContentTitle("TITLE").setContentText("CONTENT").setContentInfo("INFO") 
      .setWhen(System.currentTimeMillis()).setAutoCancel(false) 
      .setContentIntent(pendIntent); 

    Notification notice = builder.build(); 

    startForeground(startForegroundId, notice); 

我拿到旁邊的圖標是:MyApp的運行 - 到目前爲止好,但他們我得到的標準文本:「觸摸了解更多信息或停止.. 。「 而且 - 當用戶點擊應用程序信息標準對話框中的圖標時。

如何更改文字? 如何在用戶點擊圖標時運行我的MainActivity?裏面onStartCommand

回答

1

寫婁代碼(服務)的方法:

Notification note = new Notification(R.drawable.ic_launcher, 
      "Foreground Service notification?", System.currentTimeMillis()); 
    Intent i = new Intent(this, CurrentActivity.class); 
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP 
      | Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent pi = PendingIntent.getActivity(this, 0, i, 0); 
    Date dateService=new Date(System.currentTimeMillis()); 
    String dateString=dateService.toString().split(" ")[1]+" "+dateService.toString().split(" ")[2]+" "+dateService.toString().split(" ")[3]; 
    note.setLatestEventInfo(this, "Foreground service", 
      "Now foreground service running: "+dateString, pi); 
    note.flags |= Notification.FLAG_AUTO_CANCEL; 

    startForeground(2337, note); 
+1

這是工作不錯,但:通知構造已被棄用,以及setLatestEventInfo。什麼是讚揚和更新的方法? – DuduArbel

相關問題