我有多个应用程序和单个服务,我想创建应用程序和服务之间的通信。我正在保存应用程序发送的服务中的值。现在我试图从服务中读取另一个应用程序的相同值。如何从服务到活动完成数据传输?
这是如何实现的?我不想从服务中调用显式意图,也不想隐式意图,因为隐式意图会提供选择选项来选择所需的应用程序,这是我不想要的。请指教。
我有多个应用程序和单个服务,我想创建应用程序和服务之间的通信。我正在保存应用程序发送的服务中的值。现在我试图从服务中读取另一个应用程序的相同值。如何从服务到活动完成数据传输?
这是如何实现的?我不想从服务中调用显式意图,也不想隐式意图,因为隐式意图会提供选择选项来选择所需的应用程序,这是我不想要的。请指教。
您可以使用Broadcast Receivers来完成这项工作。你不需要后台服务来做到这一点。您可以触发一个意图,并且其他注册指定意图过滤器的应用程序也可以使用该意图。
使用广播接收器,
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
// Bundle bundle = intent.getExtras();
// String message = bundle.getString("alarm_message");
// Toast.makeText(context,bundle.getString("eventName"), Toast.LENGTH_SHORT).show();
NotificationManager notificationManager =(NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
int icon = R.drawable.event;
CharSequence notiText = "Event Notification";
long time = System.currentTimeMillis();
@SuppressWarnings("deprecation")
Notification notification = new Notification(icon, notiText,time);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
Intent notificationIntent = new Intent(context, Setting.class);
//put data in notificationIntent ....>>>>>
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context,intent.getStringExtra("eventName"),intent.getStringExtra("eventDescription"), contentIntent);
int SERVER_DATA_RECEIVED = 1;
Calendar cal=Calendar.getInstance();
Toast.makeText(context,"aavechhe "+intent.getBooleanExtra("Flag",false),Toast.LENGTH_SHORT).show();
if(intent.getIntExtra("month",0)==cal.get(Calendar.DAY_OF_MONTH))
{
Toast.makeText(context,"aavechhe "+cal.get(Calendar.DAY_OF_MONTH),Toast.LENGTH_SHORT).show();
notificationManager.notify(SERVER_DATA_RECEIVED, notification);
}
if(intent.getBooleanExtra("Flag",false))
notificationManager.notify(SERVER_DATA_RECEIVED, notification);
} catch (Exception e) {
Toast.makeText(context, "There was an error somewhere, but we still received an alarm", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
**注意:这是通知的示例,您必须在此代码中进行一些更改** – 2013-03-07 05:15:17
什么是你想分享的价值? – JPMagalhaes 2013-03-08 04:29:56