2011-10-23 3 views
0

알림을 표시하기 위해 애플리케이션에 서비스를 만들어야합니다. 나는 서비스에 대한 코드가 있습니다서비스에서 상태 표시 줄에 알림을 생성하는 중 문제가 발생했습니다. Android

package com.bba.droid; 

import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.os.IBinder; 

public class AlertsMonitorService extends Service{ 

    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    public void onCreate() { 

     super.onCreate(); 
    } 

    @Override 
    public void onStart(Intent intent, int id) { 

     NotificationManager notifyMngr=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     Notification notification=new Notification(R.drawable.icon_back, "Downloading...", System.currentTimeMillis()); 

     PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, null, 0); 

     notification.setLatestEventInfo(this.getApplicationContext(), "YoutubeDownloader", "The video is downloading...", contentIntent); 
     notifyMngr.notify(0, notification); 
    } 
} 

을 그리고 주요 활동에 대한 코드가 있습니다

package com.bba.droid; 

import android.app.Activity; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.util.Log; 

public class DroidTestActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Intent service=new Intent(); 
     service.setClass(this, AlertsMonitorService.class); 
     this.startService(service); 
    } 
} 

그러나 서비스가 상태 표시 줄에 알림을 생성하지 않습니다. 나는 어디에서 실수 했습니까?

+0

문제가 해결되었습니다. 매니페스트 파일의 서비스 레코드에 "enabled"속성을 추가하는 것을 잊었습니다. – user975290

답변

0

이 유일한 문제가 있다면 나도 몰라,하지만 당신은 당신의 의도에서 활동 null를 지정됩니다

:

PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, null, 0); 

당신은 당신의 활동을 시작할 것이다 의도를 지정하려면

Intent intent = new Intent(this, DroidTestActivity.class); 
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent, 0); 
관련 문제