Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从服务启动一个片段_Java_Android_Android Intent_Android Fragments_Android Service - Fatal编程技术网

Java 从服务启动一个片段

Java 从服务启动一个片段,java,android,android-intent,android-fragments,android-service,Java,Android,Android Intent,Android Fragments,Android Service,我的活动包含3个选项卡,每个选项卡都是一个片段 我还有一个服务,它可以逐次查询数据库。根据查询结果,我会发出通知(来自服务) 当我点击通知时,是否可以启动特定片段?如果是,我怎样做 以下是我在服务课程中迄今为止所做的工作: // When notification is clicked, go back to TabOperations Fragment Intent i = new Intent(this, TabOperations.class); PendingIntent pi = Pe

我的
活动
包含3个选项卡,每个选项卡都是一个
片段

我还有一个
服务
,它可以逐次查询数据库。根据查询结果,我会发出
通知
(来自
服务

当我点击
通知时
,是否可以启动特定片段?如果是,我怎样做

以下是我在
服务
课程中迄今为止所做的工作:

// When notification is clicked, go back to TabOperations Fragment
Intent i = new Intent(this, TabOperations.class);
PendingIntent pi = PendingIntent.getActivity(this, 0,  i,PendingIntent.FLAG_UPDATE_CURRENT); // Give the phone access to the app
notification.setContentIntent(pi);

// Issue notification
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
nm.notify(UNIQUE_ID, notification.build());
当然,这是行不通的


有什么建议吗

与其使用
服务
发送
通知
,不如使用不同的消息传递系统。例如,您可以利用观察者模式(或类似的第三方库)将消息从服务发送到关注此类事件的活动。然后,活动可以根据需要使用事件启动并附加片段。

否您不能“启动片段”。活动外部不存在片段。要解决您的问题,请启动包含3个选项卡的活动,并传入一个intent参数,该参数告诉活动应该导航到哪个选项卡。然后,活动将侦听此意图参数并对其作出反应。当然,您必须自己编写所有这些代码。请参阅PendingEvent文档,了解哪些android组件可以通过通知进行通知