我是Android的新手.我阅读了Android文档,但我仍需要进一步澄清.谁能告诉我究竟PendingIntent是什么?
我正在创建一个前台服务,其中包含服务启动时通知栏中显示的通知.如果服务停止,通知将解除.我的问题是,当"清除所有通知"或解除通知(滑动)时,有没有办法停止服务?
更新以包括通知的实施:
public int onStartCommand(Intent intent, int flags, int startId)
{
Log.d(CLASS, "onStartCommand service started.");
if (getString(R.string.service_start_action) == intent.getAction())
{
Intent intentForeground = new Intent(this, ServiceMain.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intentForeground, 0);
Notification notification;
Notification.Builder builder = new Notification.Builder(getApplicationContext())
.setSmallIcon(android.R.drawable.btn_star)
.setTicker("Service started...")
.setContentIntent(pendIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setOnlyAlertOnce(true)
.setOngoing(false);
notification = builder.build();
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
startForeground(SERV_NOTIFY, notification);
player.start();
}
else
{
Log.d(CLASS, "onStartCommand unable to identify acition.");
}
return START_STICKY;
}
Run Code Online (Sandbox Code Playgroud) android ×2