2013-03-07 75 views
0

我有多個應用程序和單個服務,我想創建應用程序和服務之間的通信。我正在保存應用程序發送的服務中的值。現在我試圖從服務中讀取另一個應用程序的相同值。如何從服務到活動完成數據傳輸?

這是如何實現的?我不想從服務中調用顯式意圖,也不想隱式意圖,因爲隱式意圖會提供選擇選項來選擇所需的應用程序,這是我不想要的。請指教。

+0

什麼是你想分享的價值? – JPMagalhaes 2013-03-08 04:29:56

回答

0

您可以使用Broadcast Receivers來完成這項工作。你不需要後臺服務來做到這一點。您可以觸發一個意圖,並且其他註冊指定意圖過濾器的應用程序也可以使用該意圖。

0

使用廣播接收器,

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(); 

     } 
    } 
} 
+0

**注意:這是通知的示例,您必須在此代碼中進行一些更改** – 2013-03-07 05:15:17

相關問題