Android 如何从后台服务向活动发送数据?

Android 如何从后台服务向活动发送数据?,android,socket.io,background-service,Android,Socket.io,Background Service,我想从后台服务向应用程序活动发送数据。我如何才能做到这一点?我在后台服务中使用socketio获取数据。我需要在我的活动类中以友好方式使用这些数据 在您的服务中 private void sendMessageToActivity(String newData){ Intent broadcastIntent = new Intent(); broadcastIntent.setAction("ServiceToActivityAction");

我想从后台服务向应用程序活动发送数据。我如何才能做到这一点?我在后台服务中使用socketio获取数据。我需要在我的活动类中以友好方式使用这些数据

在您的
服务中

private void sendMessageToActivity(String newData){
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("ServiceToActivityAction");
        broadcastIntent.putExtra("ServiceToActivityKey", newData);
        sendBroadcast(broadcastIntent);
}
private void scheduleAlarm()
{
    // acquire wakelock
    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    mWakeLock.acquire();
    try{
    // The time at which the alarm will be scheduled. 
    // example every 30 secs = 30000 long as time
    Long time = timeToFetchData;

    // Create an Intent and set the class that will execute when the Alarm triggers. Here we have
    // specified AlarmReceiver in the Intent. The onReceive() method of this class will execute when the broadcast from your alarm is received.
    Intent intentAlarm = new Intent(this, AlarmReceiver.class);

    // Get the Alarm Service.
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    // Set the alarm for a particular time.
    alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
    Toast.makeText(this, "Alarm Scheduled for 30 seconds", Toast.LENGTH_LONG).show();       
   }catch(Exception ex){}
   finally{
      // release wake lock
      mWakeLock.release();

   }
}

public class AlarmReceiver extends BroadcastReceiver
{
     @Override
     public void onReceive(Context context, Intent intent)
     {

         // Your code to execute when the alarm triggers
         // and the broadcast is received.   
         // perform your operations here.

         // we need to reschedule again
         // set the timeToFetchData
         // call scheduleAlarm
         timeToFetchData = 30000;
         scheduleAlarm();
     }
}
在您的
活动中

private ServiceToActivity serviceReceiver;
@Override  
public void onCreate(Bundle savedInstanceState)   
{  
...  

    serviceReceiver = new ServiceToActivity();  
    IntentFilter intentSFilter = new IntentFilter("ServiceToActivityAction");  
    registerReceiver(serviceReceiver, intentSFilter);  



...  
}  

public class ServiceToActivity extends BroadcastReceiver  
{  
    @Override   
    public void onReceive(Context context, Intent intent)  
    {  
         Bundle notificationData = intent.getExtras();  
         String newData  = notificationData.getString("ServiceToActivityKey");  

         // newData is from the service

    }  
 }

@Override  
protected void onDestroy()   
{  
    ...  

    unregisterReceiver(serviceReceiver);  


    ...  
}
在您的
AndroidManifest.xml

<manifest ... >
  ...
  <application ... >
      <service android:name="com.your-package.ServiceToActivity" />
      ...
  </application>
</manifest>
希望有帮助:)

使用

在您的
服务中

private void sendMessageToActivity(String newData){
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("ServiceToActivityAction");
        broadcastIntent.putExtra("ServiceToActivityKey", newData);
        sendBroadcast(broadcastIntent);
}
private void scheduleAlarm()
{
    // acquire wakelock
    final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
    mWakeLock.acquire();
    try{
    // The time at which the alarm will be scheduled. 
    // example every 30 secs = 30000 long as time
    Long time = timeToFetchData;

    // Create an Intent and set the class that will execute when the Alarm triggers. Here we have
    // specified AlarmReceiver in the Intent. The onReceive() method of this class will execute when the broadcast from your alarm is received.
    Intent intentAlarm = new Intent(this, AlarmReceiver.class);

    // Get the Alarm Service.
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);

    // Set the alarm for a particular time.
    alarmManager.set(AlarmManager.RTC_WAKEUP, time, PendingIntent.getBroadcast(this, 1, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));
    Toast.makeText(this, "Alarm Scheduled for 30 seconds", Toast.LENGTH_LONG).show();       
   }catch(Exception ex){}
   finally{
      // release wake lock
      mWakeLock.release();

   }
}

public class AlarmReceiver extends BroadcastReceiver
{
     @Override
     public void onReceive(Context context, Intent intent)
     {

         // Your code to execute when the alarm triggers
         // and the broadcast is received.   
         // perform your operations here.

         // we need to reschedule again
         // set the timeToFetchData
         // call scheduleAlarm
         timeToFetchData = 30000;
         scheduleAlarm();
     }
}
在您的
活动中

private ServiceToActivity serviceReceiver;
@Override  
public void onCreate(Bundle savedInstanceState)   
{  
...  

    serviceReceiver = new ServiceToActivity();  
    IntentFilter intentSFilter = new IntentFilter("ServiceToActivityAction");  
    registerReceiver(serviceReceiver, intentSFilter);  



...  
}  

public class ServiceToActivity extends BroadcastReceiver  
{  
    @Override   
    public void onReceive(Context context, Intent intent)  
    {  
         Bundle notificationData = intent.getExtras();  
         String newData  = notificationData.getString("ServiceToActivityKey");  

         // newData is from the service

    }  
 }

@Override  
protected void onDestroy()   
{  
    ...  

    unregisterReceiver(serviceReceiver);  


    ...  
}
在您的
AndroidManifest.xml

<manifest ... >
  ...
  <application ... >
      <service android:name="com.your-package.ServiceToActivity" />
      ...
  </application>
</manifest>

希望有帮助:)

使用阅读绑定服务,当使用本地服务时,您只需在活动中共享服务的活页夹使用阅读绑定服务,当使用本地服务时,您只需在活动中共享服务的活页夹我想我遗漏了一些东西。
ServiceToActivity
是活动的内部类吗?在这种情况下,要将
ServiceToActivity
添加到清单中,它必须是静态的?我想我缺少了一些东西。
ServiceToActivity
是活动的内部类吗?在这种情况下,要将
ServiceToActivity
添加到清单中,它必须是静态的?