0
Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),System.currentTimeMillis());
    Intent notificationIntent = new Intent(this, ExampleActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
    notification.setLatestEventInfo(this, getText(R.string.notification_title),getText(R.string.notification_message), pendingIntent);
    startForeground(ONGOING_NOTIFICATION_ID, notification);

我的 minSdkVersion="8" 所以我在执行上面的代码时出错。我尝试使用支持库来做到这一点

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
    NotificationCompat.Builder mBuilder =new  NotificationCompat.Builder(getBaseContext());
    mBuilder
    .setContentTitle("Service")
    .setContentText("Service is running")
    .setSmallIcon(R.drawable.icon)
    .setContentIntent(pendingIntent)
    .build();
    //now how to perform startForeground

我需要执行 startForeground(ONGOING_NOTIFICATION_ID, notification),但我没有通知对象。我还需要做 notification.setLatestEventInfo(.....)

4

0 回答 0