为什么在IntentService中super.OnCreate删除通知startForeground()

时间:2016-07-21 10:10:21

标签: android foreground-service

我正在接受Android研究,我有一些问题。

我有这个intentService:

public class SystemService extends IntentService{
    public SystemService() {
       super("SystemService");
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
       super.onStartCommand(intent, startId, startId);
       return(START_NOT_STICKY);
    }

    @Override
    public void onCreate(){
        super.onCreate();
        startForeground();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void startForeground(){
    Intent notificationIntent = new Intent(this, DashboardActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    Notification noti = new Notification.Builder(getApplicationContext())
            .setContentTitle("Foregroundservice")
            .setContentText("foreground is active")
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentIntent(pendingIntent).build();
    startForeground(1337, noti);
    }
}

为什么,在onCreate()函数中,如果我把函数startForeground()这样,在super.onCreate()之前初始化foregroundservice,前台通知就会消失但是如果put之后看起来有用吗?

0 个答案:

没有答案