2012-08-27 40 views
2

我有兩個活動A和B.當應用程序啓動時啓動A.我這是從A.待處理意圖不能在由服務創建的通知中工作

這是我在活動A.

Button btnStart = (Button) findViewById(R.id.button1); 
    btnStart.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      startService(new Intent(getBaseContext(), Service_class.class)); 

     } 
    }); 

碼推出下面是我的服務onStartCommand方法的服務。

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    createNotification(); 
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show(); 
    return START_STICKY; 
} 

這裏的createNotification方法

private void createNotification() { 

    NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    Intent nintent = new Intent(); 
    nintent.setClass(this, TestActivity.class); 
    PendingIntent pin = PendingIntent.getActivity(getApplicationContext(), 
      0, nintent, 0); 
    String title = "KR Darsan"; 
    String body = "This is a notification from darsh"; 
    Notification n = new Notification(R.drawable.ic_launcher, body, 
      System.currentTimeMillis()); 
    n.contentIntent = pin; 
    n.setLatestEventInfo(getApplicationContext(), title, body, pin); 

    n.defaults = Notification.DEFAULT_ALL; 
    nm.notify(unique_id, n); 

} 

我設置的未決意圖活動B(測試活動)。通知根據需要顯示,但是當我點擊通知時,活動不會啓動。

我在清單中聲明瞭該服務。

<service android:name=".Service_class" /> 

有什麼我應該在清單中聲明?可能是什麼問題呢 ?

回答

8

您是否在AndroidManifest.xml中聲明TestActivity.class

+2

我的不好!感謝您指出。我總是犯下愚蠢的錯誤。 – darsh

+0

你是說TestActivity.class應該在BOTH清單中列出嗎?即,在活動A和活動B的清單中? – PeteH

+0

活動是應用程序的一個窗口,而不是應用程序本身。應用程序或庫項目只能有一個清單。 – Auriukas