我有一项服务可以开始通知:
public class Notify extends Service{
@Override
public void onCreate() {
Intent resultIntent = new Intent(this, Main.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification noti_builder = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContentTitle("Title")
.setContentText("Text")
.setSound(uri)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti_builder.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti_builder);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
服务在其他课程中开始:
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancelAll();
Intent intent = new Intent(AddAlarm.this, Notify.class);
intent.putExtra("id", ID);
AlarmManager manager = (AlarmManager)getSystemService(Activity.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(AddAlarm.this, 0, intent, 0);
manager.set(AlarmManager.RTC_WAKEUP, notifyDate.getTimeInMillis(), pendingIntent);
我想在服务中获得额外服务。有什么建议怎么做?谢谢。
答案 0 :(得分:1)
在Notify类中,将代码从onCreate方法移动到具有以下签名的onStartCommand方法
onStartCommand (Intent intent, int flags, int startId)
然后你可以从传递给方法的意图中访问额外内容。