如何从后台服务向Activity发送数据?

erg*_*ran 1 android background-service socket.io

我想从后台服务向app活动发送数据.我该怎么做?我在后台服务上使用socketio获取数据.我需要在我的活动类中使用这些数据

Spu*_*dow 7

使用 BroadcastReceiver

在你的 Service

private void sendMessageToActivity(String newData){
        Intent broadcastIntent = new Intent();
        broadcastIntent.setAction("ServiceToActivityAction");
        broadcastIntent.putExtra("ServiceToActivityKey", newData);
        sendBroadcast(broadcastIntent);
}
Run Code Online (Sandbox Code Playgroud)

在你的 Activity

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


    ...  
}
Run Code Online (Sandbox Code Playgroud)

在你的 AndroidManifest.xml

<manifest ... >
  ...
  <application ... >
      <service android:name="com.your-package.ServiceToActivity" />
      ...
  </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)

使用AlarmManager安排工作.你的内心Service

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();
     }
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你 :)