gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 14:37:48 25 4
gpt4 key购买 nike

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

最佳答案

使用BroadcastReceiver

在您的服务

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

在你的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);


...
}

在你的AndroidManifest.xml

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

使用 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();
}
}

希望对你有帮助:)

关于android - 如何从后台服务向 Activity 发送数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25332078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com