【发布时间】:2016-07-09 01:20:51
【问题描述】:
我希望在单击通知操作按钮时执行一些方法。 我在这个网站上进行了搜索,但一切似乎都井井有条,而且我的 IntentService 没有被调用。
我的操作按钮意图
Intent off = new Intent();
off.setAction("action");
off.putExtra("test", "off");
PendingIntent pOff = PendingIntent.getService(context, 22, off, 0);
通知生成器
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(/**/)
.setContentTitle(/**/)
.setContentText(/**/)
.addAction(/**/, "Off", pOff)
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true);
意图服务类
public class NotificationServiceClass extends IntentService {
public NotificationServiceClass(String name) {
super(name);
}
public NotificationServiceClass () {
super("NotificationServiceClass");
}
@Override
protected void onHandleIntent(Intent intent) {
Log.i("test", "onHandle");
if (intent.getAction().equals("action")) {
Log.i("test", "action");
Bundle bundle = intent.getExtras();
if (bundle != null) {
Log.i("test", "onHandleBundleNotNull");
if (bundle.containsKey("test")) {
Log.i("test", bundle.getString("test"));
}
}
}
}
}
服务类的 XML 声明
<service
android:name=".Manager.NotificationServiceClass"
android:exported="false">
</service>
【问题讨论】:
标签: android android-intent android-notifications intentservice