我正在做一个android应用程序,我想从活动中获取数据到服务 ,然后此服务将发出通知并将数据发送到其他活动,用户可以看到结果活动?请HEP我在活动和服务之间共享数据android
-4
A
回答
0
写在活动从那里你要发送的数据:(MSG是您要发送的数据)的接收机活动
Intent myintent = new Intent("package name of class where you want to send");
myintent.putExtra("message", msg);
myintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//You might need this
getApplicationContext().sendBroadcast(myintent);
在的onCreate(): 补充一点:(任何地方设置内容视图)后
getApplicationContext().registerReceiver(broadcastReceiver, new IntentFilter("package name of current class i.e receiver"));
类补充一点:
private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Data received is : " + intent.getStringExtra("message"));
//Make a Notification here
}
};
铝所以(在接收器活动中):
@Override
public void onResume() {
super.onResume();
getApplicationContext().registerReceiver(broadcastReceiver, new IntentFilter("package name of current class i.e receiver"));
}
protected void onPause() {
super.onPause();
getApplicationContext().unregisterReceiver(broadcastReceiver);
}
+0
你是如何发送消息到服务的? –
+0
GCM !! Handler怎么样? –
+0
看看这[问题](http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging) –
相关问题
- 1. 在本地服务和托管活动之间共享数据
- 2. 在服务和活动之间共享数据
- 3. 在服务和活动之间共享数据
- 4. 在活动和服务之间共享数据库
- 5. 在AngularJS服务之间共享数据
- 6. 在微服务之间共享数据
- 7. 在Web服务之间共享数据
- 8. 在活动和服务之间共享偏好
- 9. Android中的碎片和活动之间的数据共享
- 10. 在活动和服务之间分享大量数据
- 11. 在AppEngine和Android之间共享数据
- 12. Android工作室 - 在活动之间共享数据
- 13. 在Android中的两个活动之间共享数据库
- 14. Android:在特定流程和活动中的服务之间共享对象
- 15. Android - 活动之间的共享网络
- 16. asynctask活动之间共享
- 17. 在活动和类别之间共享数据
- 18. 如何在活动和小部件之间共享数据?
- 19. 如何在活动和ContentProvider之间共享数据库?
- 20. 如何在活动/服务之间共享资源
- 21. Android:在服务和活动之间传递数据
- 22. Android活动之间共享数据的两种不同方法
- 23. 在C#服务器进程和网站之间共享数据
- 24. 在Android用户之间共享数据
- 25. 活动和片段之间共享数据
- 26. 在服务间共享数据
- 27. 在活动之间共享域对象
- 28. 在活动之间共享WebView
- 29. 在活动之间共享DBAdapter
- 30. 在活动之间共享菜单栏
你还尝试过什么吗? – FWeigl
看看这个[问题](http://stackoverflow.com/questions/4300291/example-communication-between-activity-and-service-using-messaging) –
我想首先从活动A获取数据到服务S并将数据发送到活动D? –