IntentService onHandleEvent() 未启动
IntentService onHandleEvent( ) not starting
我希望在单击通知操作按钮时执行某些方法。
我在这个网站上搜索过,但似乎一切正常,我的 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);
意向服务Class
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 服务声明 class
<service
android:name=".Manager.NotificationServiceClass"
android:exported="false">
</service>
根据 Intents and Intent Filters training,您构建的 Intent 是一个隐式 Intent:
Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.
您真正想要的是一个明确的意图:根据同一页面上的注释指定要以名称开头的组件:
Note: When starting a Service, you should always specify the component name. Otherwise, you cannot be certain what service will respond to the intent, and the user cannot see which service starts.
构建 Intent 时,您应该使用
// Note how you explicitly name the class to use
Intent off = new Intent(context, NotificationServiceClass.class);
off.setAction("action");
off.putExtra("test", "off");
PendingIntent pOff = PendingIntent.getService(context, 22, off, 0);
在查看您的代码时,我没有看到您告诉 PendingIntent 什么 class 用于您的服务。
您应该添加:
off.setClass(this, NotificationServiceClass.class);
否则PendingIntent无事可做。
我希望在单击通知操作按钮时执行某些方法。 我在这个网站上搜索过,但似乎一切正常,我的 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);
意向服务Class
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 服务声明 class
<service
android:name=".Manager.NotificationServiceClass"
android:exported="false">
</service>
根据 Intents and Intent Filters training,您构建的 Intent 是一个隐式 Intent:
Implicit intents do not name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. For example, if you want to show the user a location on a map, you can use an implicit intent to request that another capable app show a specified location on a map.
您真正想要的是一个明确的意图:根据同一页面上的注释指定要以名称开头的组件:
Note: When starting a Service, you should always specify the component name. Otherwise, you cannot be certain what service will respond to the intent, and the user cannot see which service starts.
构建 Intent 时,您应该使用
// Note how you explicitly name the class to use
Intent off = new Intent(context, NotificationServiceClass.class);
off.setAction("action");
off.putExtra("test", "off");
PendingIntent pOff = PendingIntent.getService(context, 22, off, 0);
在查看您的代码时,我没有看到您告诉 PendingIntent 什么 class 用于您的服务。
您应该添加:
off.setClass(this, NotificationServiceClass.class);
否则PendingIntent无事可做。