0

我正在继续进行 Android 研究,但我有一个问题。

我有这个意图服务:

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 after 似乎有效?

4

0 回答 0